Reputation: 1147
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
Upvotes: 2
Views: 1625
Reputation: 17474
You used
#pragma unmanaged
So, you can't use any managed code there.
Upvotes: 1