Call .Net Method from VC++

B

BigZero

Hello ppl,

Is ther way to call VB NET method from VC++.

I have VB net 2005 code that as some method and i want to call method
from VC ++ how to do this or is it possible tha twe can call method of
dot net from VC++




Thanks
Vm
 
T

Tom Shelton

Hello ppl,

Is ther way to call VB NET method from VC++.

I have VB net 2005 code that as some method and i want to call method
from VC ++ how to do this or is it possible tha twe can call method of
dot net from VC++




Thanks
Vm

It is possible - via C++/CLI. Can you be a little more specific about your
scenario?
 
M

Michel Posseth [MCP]

If you mean legacy C++ , yes you can call from both sides through COM
implement a COM / ACTIVEX interface in C++ and / or a COM interface in
VB.Net

hth

Michel
 
B

BigZero

Thanks for the reply,

well here is the problem,

i have third party software that is built on VC++ / C++ that has com
object all.
My application is on VB net 2005 and it transfer some data from My
application to the Third party software.

i written a dll that communicates between the third party and my
application,
currently there is one way communication that is i can call function
in C++ from VB net application and it's working fine for me.

i wanted to call the VB / C# net method from c++ (DLL is under third
party space).

the communication dll is stored / under the third party software space
and imported to vb net application.

so i wanted to call the vb net method from c++ ( dll is under third
party space).

hope this make my problem clear or if you want any more information.


Thanks
Vm
 
T

Tom Shelton

Thanks for the reply,

well here is the problem,

i have third party software that is built on  VC++ / C++ that has com
object all.
My application is on VB net 2005 and it transfer some data from My
application to the Third party software.

i written a dll that communicates between the third party and my
application,
currently there  is one way  communication that is i can call function
in C++ from VB net application and it's working fine for me.

i wanted to call the VB / C#  net method from c++ (DLL is under third
party space).

the communication dll is stored / under the third party software space
and imported to vb net application.

so i wanted to call the vb net method from c++ ( dll is under third
party space).

hope this make my problem clear or if you want any more information.

Thanks
Vm

Hmm... Well, If I understand what your asking, check out this little
walk through I created for another discussion:
http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/

I don't claim to be an expert at this, but I have done this a couple
of times, and it seemed to work ok :)
 
B

BigZero

Thanks You this relay works
I don't claim to be an expert at this, but I have done this a couple

It show that you have something thanks agian



Thanks
Vm
 
B

BigZero

Hello Tom Shelton ,

small problem has i told that my dll is under other software space,
your code works fine when ever thing is under same space, but when i
try to load it from other space it cause me error.

This what i tried in CallCSMethod.CPP file but it gives the following
error

Code:
HMODULE hMod = LoadLibrary(_T("D:\MyDll.dll"));

[Exception]
(*hMod).unused CXX0030: Error: expression cannot be evaluated
[/Exception]

This won't work with other space or may i need change some settings.


Thanks
Vm
 
B

BigZero

opps sorry , this bug is solved previously i m stuck over here.

Code:
HMODULE hMod = LoadLibrary(_T("D:\\MyDll.dll"));

[Exception]
Unhandled exception at 0x7c812aeb in
TestCallCSharpMethod.exe: 0xE0434F4D: 0xe0434f4d.
[/Exception]


Any help

Thanks
Vm
 
T

Tom Shelton

opps sorry , this bug is solved previously i m stuck over here.

Code:
HMODULE hMod = LoadLibrary(_T("D:\\MyDll.dll"));

[Exception]
Unhandled exception at 0x7c812aeb in
TestCallCSharpMethod.exe: 0xE0434F4D: 0xe0434f4d.
[/Exception]


Any help

Thanks
Vm

MyDll.dll has a dependancy (the managed code dll). That dependancy will not
be loaded, because it won't be in the dll search path.

You can try appending the directory that MyDll.dll resides in to the PATH
environment variable - since by default windows will search this path for
dependancies...
 
B

BigZero

i m sorry but i m not getting what exactly you mean, you want me to
keep the dependency dll in GAC folder.
but in GAC there are only .MSIL files are there.



Thanks
Vm
 
T

Tom Shelton

i m sorry but i m not getting what exactly you mean, you want me to
keep the dependency dll in GAC folder.
but in GAC there are only .MSIL files are there.



Thanks
Vm

Ok... Let's see if I can clarify a little :)

In the walk-through, I created a mixed mode dll (managed + unmanaged) called
MyDll.dll. MyDll.dll has a reference to the managed library
CustomCSharpLib.dll. When in the original example, I call LoadLibrary on
MyDll.dll - Windows automatically finds and loads CustomCSharpLib.dll, because
everything is in it'd default search path. The default search path is:

http://msdn.microsoft.com/en-us/library/7d83bc18.aspx
<quote>
1. The directory where the executable module for the current process is located.

2. The current directory.

3. The Windows system directory. The GetSystemDirectory function retrieves the
path of this directory.

4. The Windows directory. The GetWindowsDirectory function retrieves the path
of this directory.

5. The directories listed in the PATH environment variable.
</quote>

So, if MyDll.dll and CustomCSharpLib.dll are not in the same directory, then
you need to provide windows a way to find CustomCSharpLib.dll. As I see it,
you have three choices:

1. You put CustomCSharpLib.dll in the gac
2. You modify your processes PATH environment variable and append the
directory that CustomCSharpLib.dll resides in.
3. You call the SetDllDirectory API (if you can assure you are on XP SP1 or
above).

I think, the easiest is to just put CustomCSharpLib.dll in the gac. I'm 99%
sure that will take care of the issue. My confidence rating on the other two
options is more like 80% :)
 
B

BigZero

1. You put CustomCSharpLib.dll in the gac
it's works thatnks, but in real example i have more then one dll that
all are inter dependent , so that i can load it in GAC folder.
so i m trying the other 2 options but

2. You modify your processes PATH environment variable and append the
directory that CustomCSharpLib.dll resides in.

3. You call the SetDllDirectory API (if you can assure you are on XP
SP1 or
above).

i did tried this things but it did not worked, so i m working load
some who the third dll that is CustomCSharpLib.dll
to Mydll.dll from current folder that is the MyDll.dll must load it
from the current folder not from GAC or some wher else.

thanks for your help.


Thanks
Vm
 
B

BigZero

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
 
T

Tom Shelton

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

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.
 
B

BigZero

Well thanks for that, i never said that there is problem with your
code,
your code works correctly,
as you told we need to restructure the code that i already done and
the function is working correctly now,



Thanks for your Help

Vm
 
T

Tom Shelton

Well thanks for that, i never said that there is problem with your
code,
your code works correctly,
as you told we need to restructure the code that i already done and
the function is working correctly now,



Thanks for your Help

Vm

Hey, no problem. I hope you get it working :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top