468,772 Members | 1,843 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,772 developers. It's quick & easy.

Using a C++ DLL from VB, and Passing Pointers?

Hello,

I'm using a DLL I wrote in C++, and am attempting to call and use it
from VB.

This works fine for functions where I pass parameters by value, but I
can't get pointers to work.

I get the following error in the VB.net application:
"An unhandled exception of Type
'System.Runtime.InteropServices.MarshalDirectiveEx ception' occurred in
Pendant.exe "PInvoke restriction: can not return variants""
For the dll, GamePad.dll, I have functions set up thus:

#define DllExport extern "C" __declspec(dllimport) __stdcall

void DllExport TestFunction(int *pv)
{
*pv = 987; // just pass back any arbitrary value
}
In Visual Basic I have the function imported thus:

<DllImport("gamepad.dll")> Public Shared Function TestFunction(ByRef v
As Integer)
End Function

Then, I use the function in a button:

Dim result As Integer
TestFunction(result)
TextBox1.Text = result.ToString
The error occurs right at the TestFunction() call.

Does anyone have any ideas?

Thanks for any feedback.

Regards,
....John
Nov 21 '05 #1
15 13880
Try this instead:

<DllImport("gamepad.dll")> Public Shared Function TestFunction( _
ByVal ptr As IntPtr)
End Function

Private Sub TestPInvoke()
Dim result as Integer
Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf( _
GetType(Integer)))
TestFunction(ptr)
result = Marshal.ReadInt32(ptr)
TextBox1.Text = result.ToString()
End Sub
hope that helps..
Imran.

"John Alway" <jw*****@hotmail.com> wrote in message
news:db**************************@posting.google.c om...
Hello,

I'm using a DLL I wrote in C++, and am attempting to call and use it
from VB.

This works fine for functions where I pass parameters by value, but I
can't get pointers to work.

I get the following error in the VB.net application:
"An unhandled exception of Type
'System.Runtime.InteropServices.MarshalDirectiveEx ception' occurred in
Pendant.exe "PInvoke restriction: can not return variants""
For the dll, GamePad.dll, I have functions set up thus:

#define DllExport extern "C" __declspec(dllimport) __stdcall

void DllExport TestFunction(int *pv)
{
*pv = 987; // just pass back any arbitrary value
}
In Visual Basic I have the function imported thus:

<DllImport("gamepad.dll")> Public Shared Function TestFunction(ByRef v
As Integer)
End Function

Then, I use the function in a button:

Dim result As Integer
TestFunction(result)
TextBox1.Text = result.ToString
The error occurs right at the TestFunction() call.

Does anyone have any ideas?

Thanks for any feedback.

Regards,
...John

Nov 21 '05 #2
On 2004-10-13, John Alway <jw*****@hotmail.com> wrote:
Hello,

I'm using a DLL I wrote in C++, and am attempting to call and use it
from VB.

This works fine for functions where I pass parameters by value, but I
can't get pointers to work.

I get the following error in the VB.net application:
"An unhandled exception of Type
'System.Runtime.InteropServices.MarshalDirectiveEx ception' occurred in
Pendant.exe "PInvoke restriction: can not return variants""


That's because you declare it as a function, but your method really is
void in C++ so should be declared as sub...
For the dll, GamePad.dll, I have functions set up thus:

#define DllExport extern "C" __declspec(dllimport) __stdcall

void DllExport TestFunction(int *pv)
{
*pv = 987; // just pass back any arbitrary value
}
In Visual Basic I have the function imported thus:

<DllImport("gamepad.dll")> Public Shared Function TestFunction(ByRef v
As Integer)
End Function


Change this to:

<DllImport ("gamepad.dll")> _
Public Shared Sub TestFunction (ByRef v As Integer)
End Sub
--
Tom Shelton [MVP]
Nov 21 '05 #3
"Imran Koradia" <no****@microsoft.com> wrote in message news:<#9*************@TK2MSFTNGP09.phx.gbl>...
Try this instead: <DllImport("gamepad.dll")> Public Shared Function TestFunction( _
ByVal ptr As IntPtr)
End Function Private Sub TestPInvoke()
Dim result as Integer
Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf( _
GetType(Integer)))
TestFunction(ptr)
result = Marshal.ReadInt32(ptr)
TextBox1.Text = result.ToString()
End Sub

Thanks to both you and Tom Shelton. Both ways worked!

What I was hoping to do, however, was extend this pointer passing to
a structure. I thought this would be easy, however it's turning out
not to be. I can see that your method is probably the way to go for
that, because I could not get it done using ByRef.

If I have a data struct which I define in both the DLL and VB, how
can I use the method you mention and extract elements from the
structure?

Once I have the strucure size, I would then need to be able to point
to the appropriate offsets in memory to get the elements out.

If, for instance, the structure were the following

struct data {
int x;
byte y;
long z;
};

I suppose I'd use Marshal.OffSetOf() or something.
Also, is there a way to return a struct from a C function to VB?
I tried this, and was unable to do it.
Thanks for you help!

...John
Nov 21 '05 #4
John,
If I have a data struct which I define in both the DLL and VB, how
can I use the method you mention and extract elements from the
structure?

Once I have the strucure size, I would then need to be able to point
to the appropriate offsets in memory to get the elements out.

If, for instance, the structure were the following

struct data {
int x;
byte y;
long z;
};

I suppose I'd use Marshal.OffSetOf() or something.
Also, is there a way to return a struct from a C function to VB?
I tried this, and was unable to do it.


If the procedure in C is simply returning a structure, you shouldn't have to
do much - just declare the same structure in VB and declare your function to
return that structure. However, on the other hand, if the procedure is
returning a pointer to a structure, you should declare the function in VB to
return an IntPtr and then use the Marshal.PtrToStructure which will return
the structure pointed to by the pointer returned by your function. I guess
it would help if you could post some code and show what you're trying to do
so that people here can better help you.
hope that helps..
Imran.
Nov 21 '05 #5
On 2004-10-13, John Alway <jw*****@hotmail.com> wrote:
"Imran Koradia" <no****@microsoft.com> wrote in message news:<#9*************@TK2MSFTNGP09.phx.gbl>...
Try this instead:
<DllImport("gamepad.dll")> Public Shared Function TestFunction( _
ByVal ptr As IntPtr)
End Function

Private Sub TestPInvoke()
Dim result as Integer
Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf( _
GetType(Integer)))
TestFunction(ptr)
result = Marshal.ReadInt32(ptr)
TextBox1.Text = result.ToString()
End Sub

Thanks to both you and Tom Shelton. Both ways worked!

What I was hoping to do, however, was extend this pointer passing to
a structure. I thought this would be easy, however it's turning out
not to be. I can see that your method is probably the way to go for
that, because I could not get it done using ByRef.


What do you mean it didn't work? Passing the structure ByRef is exactly
what you would do..

If I have a data struct which I define in both the DLL and VB, how
can I use the method you mention and extract elements from the
structure?

Once I have the strucure size, I would then need to be able to point
to the appropriate offsets in memory to get the elements out.

If, for instance, the structure were the following

struct data {
int x;
byte y;
long z;
};


Then you would delcare it in VB.NET like so:
<StructLayout (LayoutKind.Sequential) > _
Structure data
Public x As Integer
Public y As Byte
Public z As Integer ' long in C/C++ is still 32-bit
End Structure

The StructLayout attribute is actually optional, since that is the
default - but I usually end up adding it anyway :)

Public Declare Sub MyTestFunction Lib "mytest.dll" (ByRef value As data)

....

Dim d As data
MyTestFunction (d)

This should work perfectly fine... If your having trouble, then you may
want to post some actual code that demonstrates the issue. For most
senarios, you don't have to resort to allocating unmanaged memory and
copying pointers around...

--
Tom Shelton [MVP]
Nov 21 '05 #6
Tom Shelton <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<uS**************@TK2MSFTNGP11.phx.gbl>...
On 2004-10-13, John Alway <jw*****@hotmail.com> wrote:
"Imran Koradia" <no****@microsoft.com> wrote in message news:<#9*************@TK2MSFTNGP09.phx.gbl>... [...]
If, for instance, the structure were the following

struct data {
int x;
byte y;
long z;
};
Then you would delcare it in VB.NET like so:
<StructLayout (LayoutKind.Sequential) > _
Structure data
Public x As Integer
Public y As Byte
Public z As Integer ' long in C/C++ is still 32-bit
End Structure

The StructLayout attribute is actually optional, since that is the
default - but I usually end up adding it anyway :)

Public Declare Sub MyTestFunction Lib "mytest.dll" (ByRef value As data) ... Dim d As data
MyTestFunction (d) This should work perfectly fine... If your having trouble, then you may
want to post some actual code that demonstrates the issue. For most
senarios, you don't have to resort to allocating unmanaged memory and
copying pointers around...


Thanks much. It does work for a structure. But, not for a VB
class!
I think I found the problem I was having. I need to have an array
of values in the structure, an array of longs according to the
DIJOYSTATE2 structure in DirectX 8.

I wanted to pass that back. I found that a class in VB allowed me
to create an array. But, was unable to pass that back through a
function using a DLL.

Instead I got the error:
"An unhandled exception of type 'System.ExecutionEngineException'
occurred in Pendant.exe"

If I had the C structure like this, say:

struct data
{
int x;
byte y;
int array[10];
};

Then I had to do this in VB, how could I do it? Remembering I want
to pass it back to the vb application through the DLL.
Thanks again!

...John
Nov 21 '05 #7
In article <db*************************@posting.google.com> , John Alway wrote:
Tom Shelton <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<uS**************@TK2MSFTNGP11.phx.gbl>...
On 2004-10-13, John Alway <jw*****@hotmail.com> wrote:
> "Imran Koradia" <no****@microsoft.com> wrote in message news:<#9*************@TK2MSFTNGP09.phx.gbl>... [...]
> If, for instance, the structure were the following
>
> struct data {
> int x;
> byte y;
> long z;
> };
>

Then you would delcare it in VB.NET like so:


<StructLayout (LayoutKind.Sequential) > _
Structure data
Public x As Integer
Public y As Byte
Public z As Integer ' long in C/C++ is still 32-bit
End Structure

The StructLayout attribute is actually optional, since that is the
default - but I usually end up adding it anyway :)

Public Declare Sub MyTestFunction Lib "mytest.dll" (ByRef value As data)

...

Dim d As data
MyTestFunction (d)

This should work perfectly fine... If your having trouble, then you may
want to post some actual code that demonstrates the issue. For most
senarios, you don't have to resort to allocating unmanaged memory and
copying pointers around...


Thanks much. It does work for a structure. But, not for a VB
class!
I think I found the problem I was having. I need to have an array
of values in the structure, an array of longs according to the
DIJOYSTATE2 structure in DirectX 8.

I wanted to pass that back. I found that a class in VB allowed me
to create an array. But, was unable to pass that back through a
function using a DLL.

Instead I got the error:
"An unhandled exception of type 'System.ExecutionEngineException'
occurred in Pendant.exe"

If I had the C structure like this, say:

struct data
{
int x;
byte y;
int array[10];
};


<StructLayout (LayoutKind.Sequential)> _
Structure data
Public x As Integer
Public y As Byte

<MarshalAs (UnmanagedType.ByValArray, SizeConst:=10)> _
Public array() As Integer
End Structure

Another question... Why are you using the DXAPI? Can you not use the
managed wrappers for DirectX that ship with Dx9?

--
Tom Shelton [MVP]
Nov 21 '05 #8
Tom Shelton <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<O#**************@TK2MSFTNGP12.phx.gbl>...
In article <db*************************@posting.google.com> , John Alway wrote:
[...]
If I had the C structure like this, say:

struct data
{
int x;
byte y;
int array[10];
};


<StructLayout (LayoutKind.Sequential)> _
Structure data
Public x As Integer
Public y As Byte

<MarshalAs (UnmanagedType.ByValArray, SizeConst:=10)> _
Public array() As Integer
End Structure
Thanks again, Tom. That worked beautifully! I appreciate your
knowledge level.

Another question... Why are you using the DXAPI? Can you not use the
managed wrappers for DirectX that ship with Dx9?


I don't know much about it. I'm supply the interface via a DLL,
because I know C++ and DX, and don't know much about VB. I'm
supplying joystick/gamepad input for someone else, who is going to
make it work with a robot using VB.

If I knew VB well, I'd probably simply use the calls directly within
VB. I used VB about five years ago, but I've forgotten a lot since
then, and a lot has changed.

Thanks also to Imran for his help.

...John
Nov 21 '05 #9
On 2004-10-15, John Alway <jw*****@hotmail.com> wrote:
Tom Shelton <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<O#**************@TK2MSFTNGP12.phx.gbl>...
In article <db*************************@posting.google.com> , John Alway wrote:
[...]
> If I had the C structure like this, say:
>
> struct data
> {
> int x;
> byte y;
> int array[10];
> };


<StructLayout (LayoutKind.Sequential)> _
Structure data
Public x As Integer
Public y As Byte

<MarshalAs (UnmanagedType.ByValArray, SizeConst:=10)> _
Public array() As Integer
End Structure


Thanks again, Tom. That worked beautifully! I appreciate your
knowledge level.


You're welcome. I'm glad it worked out for you...
Another question... Why are you using the DXAPI? Can you not use the
managed wrappers for DirectX that ship with Dx9?


I don't know much about it. I'm supply the interface via a DLL,
because I know C++ and DX, and don't know much about VB. I'm
supplying joystick/gamepad input for someone else, who is going to
make it work with a robot using VB.

If I knew VB well, I'd probably simply use the calls directly within
VB. I used VB about five years ago, but I've forgotten a lot since
then, and a lot has changed.

Thanks also to Imran for his help.

...John


I was just curious. I'm not a big DX expert, so I was wondering what
your reasoning was. I do believe you might find things much simpler if
you installed the DX9 sdk. It creates a DX project template for both
VB.NET and C# - in fact, with your C++ background you might find C# more
to your liking. I have a book here on managed DX9 I'm currently reading
since it is an area that has always interested me, but I have never had
much time to play with until now :)

Anyway, good luck on your project.
--
Tom Shelton [MVP]
Nov 21 '05 #10
Tom Shelton <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<uV**************@TK2MSFTNGP15.phx.gbl>...
[...]
I was just curious. I'm not a big DX expert, so I was wondering what
your reasoning was. I do believe you might find things much simpler if
you installed the DX9 sdk. It creates a DX project template for both
VB.NET and C# - in fact, with your C++ background you might find C# more
to your liking.
I was considering C#, but then I was also considering Haskell, to
give me something different to learn.
I have a book here on managed DX9 I'm currently reading
since it is an area that has always interested me, but I have never had
much time to play with until now :)
Time is exactly my problem... :) Btw, if you're interested in DX9,
there is an excellent book "Introduction to "3D Game Programming with
Direct X 9.0" by Frank D. Luna. He does use C++, however.

Anyway, good luck on your project.


Thanks.

...John
Nov 21 '05 #11
In article <db**************************@posting.google.com >, John Alway wrote:
Tom Shelton <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<uV**************@TK2MSFTNGP15.phx.gbl>...
[...]
I was just curious. I'm not a big DX expert, so I was wondering what
your reasoning was. I do believe you might find things much simpler if
you installed the DX9 sdk. It creates a DX project template for both
VB.NET and C# - in fact, with your C++ background you might find C# more
to your liking.


I was considering C#, but then I was also considering Haskell, to
give me something different to learn.


Learning something new is always a lot of fun...
I have a book here on managed DX9 I'm currently reading
since it is an area that has always interested me, but I have never had
much time to play with until now :)


Time is exactly my problem... :) Btw, if you're interested in DX9,
there is an excellent book "Introduction to "3D Game Programming with
Direct X 9.0" by Frank D. Luna. He does use C++, however.


Thanks, I'll check that out... I'm not afraid of C++. I wouldn't rate my
self as a guru, but I would put myself at least on the intermediate
level :) I actually have a simple little multiplayer game that I would
like to write, but this is an area that I just haven't studied until
now... Though it will probably have to wait until I finish my NNTP
component for the Mono/C#/GTK# based newsreader that I'm contributing to.

--
Tom Shelton [MVP]
Nov 21 '05 #12
Wow you are connected to the mono project?

Is there any hope for vb.net in this or will it strictly be C#? A friend
told me even if it is written in vb.net we can still run compiled in
mono...is this true?

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl:
In article <db**************************@posting.google.com >, John Alway
wrote:
Tom Shelton <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:<uV**************@TK2MSFTNGP15.phx.gbl>...
[...]
I was just curious. I'm not a big DX expert, so I was wondering what
your reasoning was. I do believe you might find things much simpler
if
you installed the DX9 sdk. It creates a DX project template for both
VB.NET and C# - in fact, with your C++ background you might find C#
more
to your liking.


I was considering C#, but then I was also considering Haskell, to
give me something different to learn.


Learning something new is always a lot of fun...
I have a book here on managed DX9 I'm currently reading
since it is an area that has always interested me, but I have never had

much time to play with until now :)


Time is exactly my problem... :) Btw, if you're interested in DX9,
there is an excellent book "Introduction to "3D Game Programming with
Direct X 9.0" by Frank D. Luna. He does use C++, however.


Thanks, I'll check that out... I'm not afraid of C++. I wouldn't rate
my
self as a guru, but I would put myself at least on the intermediate
level :) I actually have a simple little multiplayer game that I would
like to write, but this is an area that I just haven't studied until
now... Though it will probably have to wait until I finish my NNTP
component for the Mono/C#/GTK# based newsreader that I'm contributing
to.


Nov 21 '05 #13
Kelly,

This you can once ask to Tom Shelton, he is not busy with it now however was
that very much with it. (Probably Tom answers this when he sees this)

http://www.go-mono.com/class-status-...sualBasic.html

Cor
Nov 21 '05 #14
On 2004-10-16, scorpion53061 <ad***@nospamherekjmsolutions.com> wrote:
Wow you are connected to the mono project?

I'm not connected with the project, no. I am contributing to a
newsreader that is written using mono/C#. Sorry if that was unclear.
The only thing I've done on the mono project it self is file a few bug
reports :)
Is there any hope for vb.net in this or will it strictly be C#? A friend
told me even if it is written in vb.net we can still run compiled in
mono...is this true?


Yes, there is hope for VB.NET. It is still in serious development, and
certainly not production ready - but they are making rapid progress.
And yes, eventually, you will be able to run most VB.NET apps on mono.
I would imagine that they would have to be pure .NET apps - no com
objects - but other then that, once System.Windows.Forms is complete it
should just work. Mono and .NET to this point anyway are binary
compatible - what that means is that currently with C# apps, as long as
the class is implemented in Mono, then I can develop and compile my app
using VS.NET, transfer the exe to my linux partition, and execute it.
It goes the other way to - develop and compile on Linux, and then run on
..NET.

--
Tom Shelton [MVP]
Nov 21 '05 #15
Kelly,

I was answering on your reply and did not even look at the thread, I did not
see it was Tom who was the one you did the message to.

:-))))

Cor
Nov 21 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Morgan | last post: by
5 posts views Thread by RKS | last post: by
8 posts views Thread by Ivan Liu | last post: by
29 posts views Thread by shuisheng | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
68 posts views Thread by Jim Langston | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.