2

Im trying to learn how to write mixed code in CLI/C++.

clrHookLib.h

#pragma once
#pragma managed
using namespace System;
namespace clrHookLib {

    ref class MyClass
    {
        // TODO: Add your methods for this class here.
        public:
        static int sum(int a, int b);
    };

}

clrHookLib.cpp

#include "stdafx.h"
#include "clrHookLib.h"

int clrHookLib::MyClass::sum(int a, int b)
{
    return a + b;
}

main.cpp

#include "clrHookLib.h"
#include "Stdafx.h"

#pragma unmanaged

BOOL WINAPI DllMain(
    _In_ HINSTANCE  hInstance,
    _In_ DWORD      Reason,
    _In_ LPVOID     Reserved)
{
    switch (Reason)
    {
        case DLL_PROCESS_ATTACH:
        {
            int b = clrHookLib::MyClass::sum(1, 2);
            std::string str = std::to_string(b);
            MessageBoxA(0, str.c_str, "result from managed code!!", MB_OK);
            break;
        }
    }
}

While compilling Visual studio shows me an errors:

Error   2   error C2653: 'clrHookLib' : is not a class or namespace name    C:\Users\*\Documents\Visual Studio 2013\Projects\clrHookLib\clrHookLib\Main.cpp 15  1   clrHookLib
Error   3   error C3861: 'sum': identifier not found    C:\Users\*\Documents\Visual Studio 2013\Projects\clrHookLib\clrHookLib\Main.cpp 15  1   clrHookLib
Error   4   error C3867: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str': function call missing argument list; use '&std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str' to create a pointer to member C:\Users\*\Documents\Visual Studio 2013\Projects\clrHookLib\clrHookLib\Main.cpp 17  1   clrHookLib

The question is why compiller cant find clrHookLib namespace? What im doing wrong?

Thanks.

[ADDED]

I have found some code at microsoft site. May be it will be useful for someone:

// initializing_mixed_assemblies.cpp
// compile with: /clr /LD 
#pragma once
#include <stdio.h>
#include <windows.h>
struct __declspec(dllexport) A {
   A() {
      System::Console::WriteLine("Module ctor initializing based on global instance of class.\n");
   }

   void Test() {
      printf_s("Test called so linker does not throw away unused object.\n");
   }
};

#pragma unmanaged
// Global instance of object
A obj;

extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) {
   // Remove all managed code from here and put it in constructor of A.
   return true;
}

I think, no comment

3
  • 2
    You are struggling with basic language syntax. But you are most of all digging yourself a deep hole you can never get out of by trying to run managed code in DllMain(). That's forbidden, that will trigger the infamous loader lock. Find a decent tutorial or book to avoid making all these mistakes. Commented Dec 19, 2013 at 14:29
  • Thanks for the answer. But can i call external managed dll to call it from my unmanaged DllMain? Commented Dec 19, 2013 at 15:30
  • Im found info: msdn.microsoft.com/ru-ru/library/ms173266.aspx Commented Dec 19, 2013 at 15:39

1 Answer 1

1

You used #pragma unmanaged

So, you can't use any managed code there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.