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 15 14793
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
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]
"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
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.
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]
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
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]
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
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]
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
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]
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.
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]
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Morgan |
last post by:
Thanks to all of you because I solved the problem related with my
previous post.
I simply made confusion with pointers to pointers and then succeeded
passing the reference to the first element...
|
by: LeTubs |
last post by:
Hi
I'm think I'm having some problem with pointers.....
namely passing them around...Or I think ? I've used
gdb to find the following (note conn is of type MYSQL *conn).
Program received...
|
by: cps |
last post by:
Hi,
I'm a C programmer taking my first steps into the world of C++.
I'm currently developing a C++ 3D graphics application using GLUT
(OpenGL Utility Toolkit written in C) for the GUI...
|
by: RKS |
last post by:
I am new to Com programming using C++. Can pointers be passed through
interface functions. I would like to pass multi dimensional arrays to
functions defined in the com interface. Any help is...
|
by: Ivan Liu |
last post by:
Hi,
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.
Ivan
|
by: shuisheng |
last post by:
Dear All,
The problem of choosing pointer or reference is always confusing me.
Would you please give me some suggestion on it. I appreciate your kind
help.
For example, I'd like to convert a...
|
by: =?Utf-8?B?U2hhcm9u?= |
last post by:
Hi Gurus,
I need to transfer a jagged array of byte by reference to unmanaged
function, The unmanaged code should changed the values of the array, and when
the unmanaged function returns I need...
|
by: Jim Langston |
last post by:
I remember there was a thread a while back that was talking about using the
return value of a function as a reference where I had thought the reference
would become invalidated because it was a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| | |