|
-
Aug 21st, 2001, 09:56 AM
#1
Thread Starter
Lively Member
SHORT Translation from C++ to VB
Hi everyone:
I'm very new to C/C++ and working with some C++ code that calls some functions from a compiled DLL that was written in C++ v.2.0 (I think). I need to call these same functions from VB. I can see the functions that are available (in Dependency Walker) but I can't get the parameters. So I thought that I would just go to the C++ code that calls the DLL functions to find out what kinds of parameters were expected.
Here's the C code:
Code:
int GridExists(char *grid_name);
Here's the code that I used:
Code:
Private Declare Function GridExists Lib "evildll.dll" (ByRef grid_name As String) As Integer
When I call the function like this:
Code:
i = GridExists("C:\MyDirectory\my_grid_file.grd")
I get the "Bad DLL Calling Convention" (error 49) message.
Anyone have a clue as to what I am doing wrong?
-
Aug 21st, 2001, 10:18 AM
#2
Member
I'm trying to learn DLLs myself, but I think using a dynamically-sized char array (char*) will not work.
-
Aug 21st, 2001, 11:12 AM
#3
Member
Yes, I know both VB and C++, but I am more strong with VB. I believe there is a macro definition for a string that you put in your DLL. But, as I am learning DLLs myself, I don't know much more.
-
Aug 21st, 2001, 11:34 AM
#4
Frenzied Member
First the function should be declared for export
int __stdcall GridExists(char *grid_name);
and put it in a *.def file. Also in C++ int is 4 bytes long, so in VB the return type will be long not integer.
-
Aug 21st, 2001, 01:05 PM
#5
Thread Starter
Lively Member
A light comes on...
Vlatko:
THANK You. I didn't know that integers were 4 bytes in C -- that clarifies a lot of things. I wondered how pointers would fit into an integer variable...
I'll give the def file a try...
-
Aug 21st, 2001, 01:29 PM
#6
Also, from my experience with external DLL fucntion calling from VB, even if the function receives a pointer or a reference you should pass it ByVal in VB. 
I have NO idea why.
-
Aug 22nd, 2001, 12:59 PM
#7
Thread Starter
Lively Member
Filburt1, Vlatko, Sc0rp:
Thanks for your input ...
Your suggestions were all very good, and I learned a lot from them (enough to solve some other issues I've been having), but I still haven't gotten it working. I'm not asking for additional help, because I think I just need to gather more information.
I did learn a few things, though. One thing that was wrong with my vb declaration was passing a string as the parameter. A "char" variable in C is not, in fact, equivalent to a vb string. If the function were expecting a string, the C declaration would have been LPSTR or LPCSTR. Where there is a char, it's expecting a byte value (or array).
Also, for everyone who has ever had a need like mine, I stumbled across an entry in the vb help files titled "Converting C Declarations to Visual Basic." It is as handy as it sounds, but still wasn't enough to help me get it working. Thanks again so much for your help, each of you. I'll post again, if I ever resolve it...
-
Aug 23rd, 2001, 08:54 AM
#8
actually, LPSTR is just an alias name for char*, and LPCSTR is an alias for const char* ( meaning an unchangeable string)
those names are only used under windows (and declared i some subinclude of windows.h)
somewhere in the reference is stated that you should pass C "char*" arguments "ByVal As String"
but maybe the problem is that the old dll functions are not declared as __stdcall. If so, all you tries to get it working are futile unless you have the original source code.
CornedBee
-
Aug 27th, 2001, 07:04 AM
#9
Thread Starter
Lively Member
Thanks, CornedBee for clearing that up. The C code that I posted in the beginning actually calls the dll successfully. I still wonder, why is it possible to access the DLL from C and not from VB?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|