Thread: [Dev-C++] help needed with calling dll from Visual basic
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ni J. <ni_...@ba...> - 2004-11-04 14:11:21
|
Hi, All.
=20
I am writing to ask for help from the Dev-C++ user community with the
problem of calling dll from visual basic.
=20
I wrote a dll by using Dev-C++ and the dll works fine with an interface
written in Dev-C++. However, when I tried to call the dll from an =
interface
written in visual basic, I run into problem.
=20
1. The dll contains a function with prototype shown below. It
concatenates the time to the string "name".
=20
Void reportTime (char *name);
=20
I call the function in dll from visual basic by using the
following declare statement
=20
Private Declare Sub reportTime Lib =
"c:\dlltest-4\dlltest-4.dll"
(ByVal name As String)
=20
I get an error message "bad dll calling convention". =20
=20
2. So I made some change to the prototype of the function in dll by
adding _stdcall as shown below.
=20
Void _stdcall reportTime (char *name);
=20
The declare statement is the same in the visual basic interface.
=20
This time, I do not get the error message shown above and I can tell =
that
the string "name" is passed into dll and the function can concatenate =
the
time to the string. However, after the dll returns the control back to =
the
interface, the string "name" in the interface does not reflect the
manipulation done in dll. I do not know what to do.=20
=20
Thank you in advance for your time and help.
=20
Jean Ni
=20
=20
|
|
From: Per W. <pw...@ia...> - 2004-11-04 14:59:37
|
Your basic program have specified the parameter as ByVal. For a normal value this means that a copy of the variable is sent as a parameter, i.e. changes will not be reflected back. Since I don't program VB, I don't know the definition of ByVal for a pointer-type parameter. If it is the pointer that is ByVal or if it is the string. In case it's the string that is ByVal, it is obvious why you don't get a change in the VB program - the changes are only to a copy of the string and are immediately thrown away on return from the function. /Per W On Thu, 4 Nov 2004, Ni Jinfeng wrote: > Hi, All. > > > > I am writing to ask for help from the Dev-C++ user community with the > problem of calling dll from visual basic. > > > > I wrote a dll by using Dev-C++ and the dll works fine with an interface > written in Dev-C++. However, when I tried to call the dll from an interface > written in visual basic, I run into problem. > > > > 1. The dll contains a function with prototype shown below. It > concatenates the time to the string "name". > > > > Void reportTime (char *name); > > > > I call the function in dll from visual basic by using the > following declare statement > > > > Private Declare Sub reportTime Lib "c:\dlltest-4\dlltest-4.dll" > (ByVal name As String) > > > > I get an error message "bad dll calling convention". > > > > 2. So I made some change to the prototype of the function in dll by > adding _stdcall as shown below. > > > > Void _stdcall reportTime (char *name); > > > > The declare statement is the same in the visual basic interface. > > > > This time, I do not get the error message shown above and I can tell that > the string "name" is passed into dll and the function can concatenate the > time to the string. However, after the dll returns the control back to the > interface, the string "name" in the interface does not reflect the > manipulation done in dll. I do not know what to do. > > > > Thank you in advance for your time and help. > > > > Jean Ni > > > > > > |
|
From: Cary H. <xen...@gm...> - 2004-11-04 15:11:13
|
I'm not an expert at this, but here are a few ideas I had: 1. since you are passing a pointer, the parameter is passed by reference, not by value. This might explain why the function seems to work, but doesn't actually change the string, as ByVal causes the function to create a copy of the string and performs its work on the copy, leaving the original intact. So, try ByRef instead of ByVal 2. since c treats strings as character arrays, you might want to try using that as the parameter, as VB might treat strings differently than character arrays. 3. try using fastcall instead of stdcall. Again, these are just ideas. Good luck! On Thu, 4 Nov 2004 08:11:10 -0600, Ni Jinfeng <ni_...@ba...> wrote: > > > > Hi, All. > > > > I am writing to ask for help from the Dev-C++ user community with the > problem of calling dll from visual basic. > > > > I wrote a dll by using Dev-C++ and the dll works fine with an interface > written in Dev-C++. However, when I tried to call the dll from an interface > written in visual basic, I run into problem. > > > > The dll contains a function with prototype shown below. It concatenates the > time to the string "name". > > > > Void reportTime (char *name); > > > > I call the function in dll from visual basic by using the > following declare statement > > > > Private Declare Sub reportTime Lib "c:\dlltest-4\dlltest-4.dll" > (ByVal name As String) > > > > I get an error message "bad dll calling convention". > > > > So I made some change to the prototype of the function in dll by adding > _stdcall as shown below. > > > > Void _stdcall reportTime (char *name); > > > > The declare statement is the same in the visual basic interface. > > > > This time, I do not get the error message shown above and I can tell that > the string "name" is passed into dll and the function can concatenate the > time to the string. However, after the dll returns the control back to the > interface, the string "name" in the interface does not reflect the > manipulation done in dll. I do not know what to do. > > > > Thank you in advance for your time and help. > > > > Jean Ni > > > > |
|
From: Carlo <car...@li...> - 2004-11-05 08:08:10
|
Ni Jinfeng ha scritto lo scorso 04/11/2004 15.11:
> Hi, All.
>
>
>
> I am writing to ask for help from the Dev-C++ user community with the
> problem of calling dll from visual basic.
>
>
>
> I wrote a dll by using Dev-C++ and the dll works fine with an interface
> written in Dev-C++. However, when I tried to call the dll from an
> interface written in visual basic, I run into problem.
>
[cut]
>
> Jean Ni
>
I have already run in this problem (this is the reason why I thrashed VB
and now use GTK+ :-); the problem is in the different use of strings
between VB and C. A workaround that worked for me is the following:
1. change your library function to:
void _stdcall reportTime (char ** name)
2. change your function declaration to:
Private Declare Sub reportTime Lib "your.dll"
(ByRef name As String)
HTH
|