Re: Call .Net Method from VC++
- From: Tom Shelton <tom_shelton@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 04 Dec 2008 07:28:22 -0800
On 2008-12-04, BigZero <vijaymajagaonkar@xxxxxxxxx> wrote:
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;
}
[/Code]
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
Windows has a specific methodology it uses to search for dependancies, and
there are only a couple of ways that I know of to modify that path. If
those aren't working for you then, you might want to ask that specific
question. This isn't a problem with .NET, VB, or the code I illustrated, but
a problem with the architecture of you application not aligning to the way
Windows searches for dependancies... The only suggestion I can make from this
point, is either to re-structure stuff so that Windows can find and load all
of your dependancies or research ways to modify the search path.
--
Tom Shelton
.
- Follow-Ups:
- Re: Call .Net Method from VC++
- From: BigZero
- Re: Call .Net Method from VC++
- References:
- Call .Net Method from VC++
- From: BigZero
- Re: Call .Net Method from VC++
- From: BigZero
- Re: Call .Net Method from VC++
- From: BigZero
- Re: Call .Net Method from VC++
- From: BigZero
- Re: Call .Net Method from VC++
- From: Tom Shelton
- Re: Call .Net Method from VC++
- From: BigZero
- Re: Call .Net Method from VC++
- From: Tom Shelton
- Re: Call .Net Method from VC++
- From: BigZero
- Re: Call .Net Method from VC++
- From: BigZero
- Call .Net Method from VC++
- Prev by Date: create a file or folder on my desktop programmatically
- Next by Date: Re: create a file or folder on my desktop programmatically
- Previous by thread: Re: Call .Net Method from VC++
- Next by thread: Re: Call .Net Method from VC++
- Index(es):
Relevant Pages
|