Using a non COM C++ DLL


B

bclegg

Hi,
I have to use a 3rd party C++ DLL say (3PDLL)
It comes with a header file that declares its interface.
The interface is a structure say (3PStruct) that has a member for each
exposed function.
We (not I)have made a C++ dll say (MyInterface)that attaches to this DLL
and exposes its public functions.

MyInterface is then used by the main VB app.

I thought it might be possible to do the same thing in C# .

The essential MyInterface C++ code is:

HINSTANCE pDll;
Blah dllAttach;
3PStruct *pMainDllRec = NULL;

pDll = LoadLibrary("3PDLL.dll");
dllAttach = (Blah)GetProcAddress(pDll, "_dll_Attach");
if(!dllAttach(&pMainDllRec, ver_number))
{
wrong version clean up and exit
}
else
{
right version, pMainRecDll is now a pointer to 3pStruct that can be
dereferenced to any of the exposed functions.
}
So now MyInterface can now:
bool MYINTERFACE ThisFunction(bool lglResult)
{
return pMainDllRec->ThatFunction(lglResult);
}

Back to the C# world:
I have managed to Loadlibrary and GetProcAddress using IntPtrs for pDLL
and dllAttach but I come unstuck after this.

MyInterface has the advantage of the provided header file so it can
declare dllAttach to be of type Blah which is a function call that takes
pMainDllRec's address and a version string.
It seems that I am stuck with declaring dllAttach as an IntPtr.
So although I can retrieve dllAttach, I can't see how make to further
use of it to set pMainDllRec.
my C# attempt:

IntPtr p = new IntPtr(0);
IntPtr dllAttach = new IntPtr(0);
IntPtr pMainDllRec = new IntPtr(0);
p=LoadLibrary(Library);
dllAttach = GetProcAddress(p,"_dll_Attach");

So far so good..

The following is what I would like to achieve next.
I guess that this functionality if possible, would be in an unsafe
procedure .

IntPtr pMainDllRec = new IntPtr(0);
string version = "1.0.0"
if (dllAttach(&pMainDllRec, version))
{
success, we have a pointer to the functions
}
else
{
failure, wrong version message etc.
}

Is this possible in C# or should I just stick with the C++ 'MyInterface'.
Thanks
Bob
 
Ad

Advertisements

Y

Ying-Shen Yu[MSFT]

Hi Bob,

Wrapping a function pointer into a delegate and use it in managed code is
not supported in .NET Framework v1.1, however it is a feature in Whidbey
release.

For now you need use managed C++ to do this since it supports IJW.

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Ad

Advertisements

B

bclegg

Hi,
Ok, Thanks.
regards
Bob

Ying-Shen Yu said:
Hi Bob,

Wrapping a function pointer into a delegate and use it in managed code is
not supported in .NET Framework v1.1, however it is a feature in Whidbey
release.

For now you need use managed C++ to do this since it supports IJW.

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 

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