Re: Call .Net Method from VC++

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
.



Relevant Pages

  • Re: Question
    ... new Class Library project and the output will be a .dll, ... Dib wrote: ... > Can you create a dll file through DOT net? ...
    (microsoft.public.dotnet.general)
  • .Net framework security Problem
    ... net.Actually i have a dot net Windows User control embedded in a web ... page (dot net ActiveX).Its working on my system but when the web page ... There are no errors reported in the Temporary Internet Files. ... Unable to load the dll in mscorcfg.msc ...
    (microsoft.public.dotnet.security)
  • Re: Call .Net Method from VC++
    ... to Dot Net dll as you told in your blog. ... typedef int ... search for the Dll into Temp folder. ... folder and other two DLL(C++ and dot net) in B-Folder ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How Can You Create A DLL In VB.NET For VB6?
    ... Yes you can write com compatible DLL withj dot net (dll class, ... > required to implement the same functionality in the vb6 app. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: am having a problem with pinvoke and StringBuilder[ ]
    ... If you don't have the source of the DLL then you are in a world of pain, that means you wont be able to use the interop marshaler, you need to "custom" marshal. ... public static extern int EnumerateBoards(ref int numBoards, ... Since the String class is immutable, I have used StringBuilder in my code ...
    (microsoft.public.dotnet.framework.clr)