C++ Call conversion

  • Thread starter Thread starter bclegg
  • Start date Start date
B

bclegg

Hi,
I am a VB.net programmer who has to make use of a 3rd party Borland C++ dll.

We have a successful VC++ wrapper that presents a number of functions
that can be declared and called in VB.net

I now want to translate the wrapper to C#.
I can find my around C# in a (pardon the pun) basic fashion.

I am looking to translate the Main routine below.
I am at the first stage of emulating getting the pointer from LoadLibrary.

I have successfully used:
[DllImport ("KERNEL32.DLL")]
public static extern long LoadLibrary(string lpLibFileName);

This passed back a large number when I fed it the dll name and it
passed back a smaller number when I fed a nonexistent name.

But the C++ code below gets a null back when passed a nonexistent name
so I figured I had to use a HINSTANCE like the C++ .

This led me to use a module and marshal.getHinstance
But I can't see how to assign the module to the dll I am interested in.

public long bcLoadLibrary(string Library)
{

long lngResult;
Module m;
//m.Name=Library; /READONLY!!

IntPtr p = new IntPtr(0);
p=Marshal.GetHINSTANCE(m);

lngResult=(long)p;
return lngResult;
}
Am I close or deep in the weeds?
Thanks
Bob
The C++ Code snippet is

HINSTANCE pScadaDll;
TfAttach dllAttach;
TsSimAPIFuncs *pMainDllRec = NULL;


BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved)
{
DWORD dwResult = 0;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
pScadaDll = LoadLibrary("TheDLL.dll");
if(pScadaDll != NULL)
{
char sMsg[500];
wsprintf(sMsg, "attached TheDLL.dll Result: ");
MessageBox(NULL, sMsg, "Success", MB_OK);
dllAttach = (TfAttach)GetProcAddress(pScadaDll, "_dll_Attach");
 
Bob,
I have successfully used:
[DllImport ("KERNEL32.DLL")]
public static extern long LoadLibrary(string lpLibFileName);

This passed back a large number when I fed it the dll name and it
passed back a smaller number when I fed a nonexistent name.

You should use IntPtr as the return type, not long.

BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved)
{
DWORD dwResult = 0;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
pScadaDll = LoadLibrary("TheDLL.dll");

The DllMain docs says explicitly that you must not call LoadLibrary
from within DllMain.



Mattias
 
Hi bclegg,

Thank you for posting in the community!

Based on my understanding, you meet the problem of P/invoke LoadLibrary in
C#.

================================
Actually, the return type of LoadLibrary is HMODULE, which in .Net you
should marshal it as IntPtr. So you should declare it as:
[DllImport ("KERNEL32.DLL")]
public static extern IntPtr LoadLibrary(string lpLibFileName);

But, use long as the return value will not cause serious problem.(Because
IntPtr is also a long type internally)

Use LoadLibrary with a nonexisted file name, the return value should be
null, which should be 0 in IntPtr.(I have tested this for you), so I can
not understand why your return value is only a "smaller number". Is the
"small number" 0?

Also, where is the main concern of you?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hello Mattias,
Thank you for your reply.
At the risk of continuing this discussion offthread,
Could you please advise where the pScadaDll = LoadLibrary("TheDLL.dll");
should be placed.
ie Is acceptable to make a subroutine call at this point which does it
or should the C++ DLL present a 'Connect' function to the interface.
Regards
Bob
Bob,

I have successfully used:
[DllImport ("KERNEL32.DLL")]
public static extern long LoadLibrary(string lpLibFileName);

This passed back a large number when I fed it the dll name and it
passed back a smaller number when I fed a nonexistent name.


You should use IntPtr as the return type, not long.


BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved)
{
DWORD dwResult = 0;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
pScadaDll = LoadLibrary("TheDLL.dll");


The DllMain docs says explicitly that you must not call LoadLibrary
from within DllMain.



Mattias
 
Hello Jeffery,
Thanks for your reply.

Your declaration works!

My main concern is that we have to interact with a
3rd party dll which uses a pointer as the way in.
ie. result = pointer-> SomeFunction

One of our programmers wrote a C++ wrapper which presents the functions
at the interface in a declarable manner.
ie.
Declare Sub SomeFunction Lib "D:\thepath\TheDLL.dll"

I would like to move this functionality into C# and present the
functions via a C# component that doesn't have to call the C++ wrapper.

Thanks
 
Hi Bob,

Based on my understanding, your dll may use some functions that is inside
another dll, so you want to load that dll for use.

I think you should specify a variable for determine the loading of that
dll, when you want to use the functions, you can judge the variable, if the
dll is not loaded, then you can load that dll. Through this way, you have
delayed the loading of the dll until your dll is initialized probably.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thank you for your reply.
I have altered the C++ wrapper.
Regards
Bob said:
Bob,

I have successfully used:
[DllImport ("KERNEL32.DLL")]
public static extern long LoadLibrary(string lpLibFileName);

This passed back a large number when I fed it the dll name and it
passed back a smaller number when I fed a nonexistent name.


You should use IntPtr as the return type, not long.


BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved)
{
DWORD dwResult = 0;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
pScadaDll = LoadLibrary("TheDLL.dll");


The DllMain docs says explicitly that you must not call LoadLibrary
from within DllMain.



Mattias
 
Hi bclegg,

Thanks very much for your feedback.

If you have any further concern, please feel free to feedback. We will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hello Jeffery,
Thanks again,
I have just have one further question if I may.
The 3rd party dll comes with a C++ header file that has some type
definitions that I can't see how to convert to C#

eg typedef void (*Tfsa_SetCurDatabase)(const char *prmDatabase);

Once it declares a number of these it then declares a struct containing
them.
One entry from which is
Tfsa_SetCurDatabase SetCurDatabase;

The struct is then exposed along with a function for version checking.
typedef bool __declspec(dllexport) (*TfAttach)(TsSimAPIFuncs
**prmSimAPIFuncs, const char *prmVersion);

extern TsSimAPIFuncs simAPI;

The C++ wrapper's global variables and connecting call are :
HINSTANCE pDll;
TfAttach dllAttach;
TsSimAPIFuncs *pMainDllRec = NULL;

void WTS_API LoadDLL()
{
DWORD dwResult = 0;
pDll = LoadLibrary("TheDLL.dll");
if(pDll != NULL)
{

dllAttach = (TfAttach)GetProcAddress(pDll, "_dll_Attach");
if(dllAttach != NULL)
{
if(!dllAttach(&pMainDllRec, CONST_API_VERSION))
...
AND away we go, either error handling or successfully exiting. If a
successful exit then the wrapper
dereferences pMainDllRec to get to the various functions in the struct.
eg
void WTS_API SetCurDatabase(const char *prmDatabase)
{
pMainDllRec->SetCurDatabase(prmDatabase);
}

My questions are:
1) Can I emulate these parameterised typedefs in C#
2) If so would you be able to show me how to write the equivalent
GetProcAddress.


regards
Bob Clegg
 
Jeffrey,
But, use long as the return value will not cause serious problem.

I think getting bogus return values is a pretty serious problem, and
that's usually the result if you incorrectly use long as the return
type.

(Because IntPtr is also a long type internally)

Not on Win32 it isn't.



Mattias
 
Hi Bob,

Sorry for letting you wait for so long time.

For callback function that is marked with "typedef", you should use
delegate to P/invoke it.

For example:

typedef void (__stdcall *PFN_MYCALLBACK)();
int __stdcall MyFunction(PFN_ MYCALLBACK callback);

You should marshal as:
public delegate void MyCallback();
[DllImport("MYDLL.DLL")]
public static extern void MyFunction(MyCallback callback);

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Bob,

Does my reply make sense to you? Is your problem resolved?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeffery,
I used a 3rd party web thing to reply but it appears to gone astray.

To be honest my C++ and C# are not strong enough to say I understand it.

But you have shown the way with your example declaration so I just need
to sit down and study it.

Are there any white papers etc on marshalling between C++ and C# that
you know of?

Thanks
again for your help.

regards
 
Hi bclegg,

Thanks very much for your feedback.

Actually, delegate in C# is somewhat like the function pointer in C,
C++(There is no point concept in C#). So for P/invoke using function
pointer, you should use delegate to marshal. For a more detailed
understanding of delegate, please refer to:
"An Introduction to Delegates"
http://msdn.microsoft.com/msdnmag/issues/01/04/net/
"Delegates, Part 2"
http://msdn.microsoft.com/msdnmag/issues/01/06/net/

For a general tutorial of P/invoke in C#, please refer to:
"Platform Invoke Tutorial"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vcwlkplatforminvoketutorial.asp

Hope this help you!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeffery,
Thanks for the links to the documentation.
Again,
I am really appreciative of the help you have supplied.
regards
Bob.
 
Back
Top