Hello ,
Tom Shelton
I have problem, need your help to solve it, has i told you that
i m trying to ladd the dll from other space it works if the . dot net dll in GAC folder but i don't want to keep it in GAC folder.
so i did this the following C++ code that loads the c++ dll and call
to Dot Net dll as you told in your blog.
Code:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
// our function pointer definition
typedef int (WINAPI* LPFN_ADDFUNC)(const int, const int);
typedef void (CALLBACK* LPFNSETDLLDIRECTORY)(LPCTSTR);
void
setDllDirectory(const char* path)
{
static LPFNSETDLLDIRECTORY MySetDllDirectory = NULL;
HMODULE hmod = GetModuleHandleA("kernel32.dll");
if (hmod != NULL) {
MySetDllDirectory = (LPFNSETDLLDIRECTORY) GetProcAddress(hmod,
"SetDllDirectoryA");
if (!MySetDllDirectory)
printf("SetDllDirectory not supported\n");
} else
printf("Error getting kernel32.dll module handle\n");
if (MySetDllDirectory) {
MySetDllDirectory((LPCTSTR)path);
}
}
int
_tmain(int argc, _TCHAR* argv[])
{
setDllDirectory("c:\\Temp");
HMODULE hMod = LoadLibrary(_T("DllLoad.dll"));
if (hMod != NULL) {
LPFN_ADDFUNC addFunc = (LPFN_ADDFUNC)
GetProcAddress(hMod, "AddTwoNumbers");
if (addFunc != NULL) {
cout << addFunc(1, 2) << endl;
cout << addFunc(5, 6) <<
endl;
cout << addFunc(100,
10024) << endl;
}
else {
cout << "Failed to load function
AddTwoNumbers" << endl;
}
FreeLibrary(hMod);
}
else {
cout << "Failed to load module MyDll.dll" << endl;
}
return 0;
}
i copied the both DLL c++ and CSharp dll into c:\Temp folder and sets
the setDllDirectory("c:\\Temp");
search for the Dll into Temp folder.
This loads the Dll at runtime but the time when we call addFun it gives the exception
[Exception]
Unhandled exception at 0x7c812aeb in
TestCallCSharpMethod.exe: 0xE0434F4D: 0xe0434f4d.
[/Exception]
This solved if i copied the Dot Net(CSharp Dll) in GAC or The C++ code A-folder, then it works
All i want to known is it possible that i keep the c++ code in A-
folder and other two DLL(C++ and dot net) in B-Folder
and call A-folder c++ code to load c++ dll from B-Folder and B-Folder c
++ dll call into current(B-Folder dot net Dll) Method.
Thanks
Vm