473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C++ Call conversion

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");

Nov 15 '05 #1
9 2139
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2

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.

Nov 15 '05 #3
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

Mattias Sjögren wrote:
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


Nov 15 '05 #4
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
Bob
Jeffrey Tan[MSFT] wrote:
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.


Nov 15 '05 #5

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.

Nov 15 '05 #6
Thank you for your reply.
I have altered the C++ wrapper.
Regards
Bob
Mattias Sjögren wrote:
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


Nov 15 '05 #7
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.

Nov 15 '05 #8
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #9
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: sjbrown8 | last post by:
I have the piece of code below, and when i try compiling with the line g++ 753075304.cpp I get the following error message: 753075304.cpp: In function 'int main()': 753075304.cpp:29: error:...
15
by: Anonymousgoogledeja | last post by:
Hi all, since the function atof, atoi, _atoi64, atol returned value are Return Values Each function returns the double, int, __int64 or long value produced by interpreting the input...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
5
by: rolandz | last post by:
Hi, Maybe somebody has been fighting with the problem that I do, currently. I have a class that has method f(). The two versions of the f() method accept different objects: Int and Short. These...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
8
by: xtrigger303 | last post by:
Hi to all, I'm working on a smart pointer implementation and I'm trying to get automatic type conversion between different pointer types. I stumbled upon something weird (at least for me) I...
7
by: cppquester | last post by:
What does this code do? #include <iostream> class A { public: A() { std::cout << "A::A()" << std::endl;} };
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
15
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
0
by: James Kanze | last post by:
On Oct 6, 3:59 pm, microcassanova <mangal.pa...@gmail.comwrote: I presume you mean explicit, but what does that change? And what is the relationship with data loss. (Most of my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.