Passing Pointers to Structs

B

Bryan G

Hi, I'm working on a VB project which involves using C
library functions which take struct pointers as args, and
I keep running into this error when trying to pass either
an IntPtr or a Structure ByRef to the functions.

An unhandled exception of
type 'System.Runtime.InteropServices.MarshalDirectiveExcept
ion' occurred in ConsoleApplication1.exe

Additional information: PInvoke restriction: can not
return variants.

Can someone please give me a general idea of what "cannot
return variants" means? The function that I've imported
takes a struct pointer and returns void.
 
T

Tom Shelton

Hi, I'm working on a VB project which involves using C
library functions which take struct pointers as args, and
I keep running into this error when trying to pass either
an IntPtr or a Structure ByRef to the functions.

An unhandled exception of
type 'System.Runtime.InteropServices.MarshalDirectiveExcept
ion' occurred in ConsoleApplication1.exe

Additional information: PInvoke restriction: can not
return variants.

Can someone please give me a general idea of what "cannot
return variants" means? The function that I've imported
takes a struct pointer and returns void.

Bryan,

It is really hard to answer a question like this without seeing both the
C definitions of the structure and function and seeing your VB
declarations. If you could post that information, I or someone else, I
am sure, would be happy to help you.
 
D

Denis C

-----Original Message-----
type 'System.Runtime.InteropServices.MarshalDirectiveExcep
t
Bryan,

It is really hard to answer a question like this without seeing both the
C definitions of the structure and function and seeing your VB
declarations. If you could post that information, I or someone else, I
am sure, would be happy to help you.
Hi Tom,

I am working with Bryan on this. Here's an example:

This a a testing struct we put in the C library:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
CHAR sval;
LONG lval;
};

These are the access functions for it.

DBACCESSDLL_API GetLongRT __cdecl GetLongVal(){
struct GetLongRTS GL;
GetLongRT temp = malloc(sizeof(struct
GetLongRTS));
return temp;
}

DBACCESSDLL_API void __cdecl SetLongVal(GetLongRT temp){
temp->sval = 'b';
}

These are the VB acess functions and the struct
declaration:

Public Structure GetLongRTS
Public sval As Char
Public lval As Integer
End Structure

<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Function GetLongVal() As IntPtr
End Function

<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Function SetLongVal(ByRef temp As
GetLongRTS)
End Function

And finally the code that when executed gives the errors
Bryan described:

Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Reflection

Module Module1
Sub Main()
Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal()
Dim obj As DBAccessFuncDecl.GetLongRTS = _
CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),
DBAccessFuncDecl.GetLongRTS)
'Dim obj As DBAccessFuncDecl.GetLongRTS
'Marshal.StructureToPtr(obj, ptr, True)
DBAccessFuncDecl.SetLongVal(obj)

Console.WriteLine(obj.lval)

End Sub
End Module

Sorry about the formatting. Let me know if you need more
info.

Separate but related question:

Are these two structures equivalent?

C code:

struct cstruct {

long l;

char c;

char *str;

int[30] ivals;

byte b;

lpvoid tmp;

char[20][30] string_array;

}



VB code:

Public Structure vbstruct

Public l As Integer

Public c As Char

Public str As StringBuilder

Public ivals As ArrayList

Public b As Byte

Public tmp As IntPtr

Public string_array As ArrayList

End Structure

Thanks,

Denis
 
T

Tom Shelton


<snip>

Ok, something to work with :). And I see a couple of things already...
Hi Tom,

I am working with Bryan on this. Here's an example:

This a a testing struct we put in the C library:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
CHAR sval;
LONG lval;
};

These are the access functions for it.

DBACCESSDLL_API GetLongRT __cdecl GetLongVal(){
struct GetLongRTS GL;
GetLongRT temp = malloc(sizeof(struct
GetLongRTS));
return temp;
}

DBACCESSDLL_API void __cdecl SetLongVal(GetLongRT temp){
temp->sval = 'b';
}

These are the VB acess functions and the struct
declaration:

Public Structure GetLongRTS
Public sval As Char
Public lval As Integer
End Structure

I'm assuming that CHAR is a typedef for char, and there for is an 8-bit
char... If that is so, then you'll want to change this to:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Structure GetLongRTS
Public sval As Char
Public lval As Integer
End
<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Function GetLongVal() As IntPtr
End Function

<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Function SetLongVal(ByRef temp As
GetLongRTS)
End Function

These declares are where your real problem lies though... You have the
C declarations using __cdecl. The default marshalling is for __stdcall.
So, you either need to change the C functions or add the
CallingConvention attribute to your VB.NET declares:

<DllImport("DBAccess.dll", CallingConvention:=CallingConvention.Cdecl, SetLastError:=True)> _
Public Shared Function GetLongVal() As IntPtr
End Function

<DllImport("DBAccess.dll", CallingConvention:=CallingConvention.Cdecl, SetLastError:=True)> _
Public Shared Function SetLongVal(ByRef temp As GetLongRTS)
End Function
And finally the code that when executed gives the errors
Bryan described:

Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Reflection

Module Module1
Sub Main()
Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal()
Dim obj As DBAccessFuncDecl.GetLongRTS = _
CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),
DBAccessFuncDecl.GetLongRTS)
'Dim obj As DBAccessFuncDecl.GetLongRTS
'Marshal.StructureToPtr(obj, ptr, True)
DBAccessFuncDecl.SetLongVal(obj)

Console.WriteLine(obj.lval)

End Sub
End Module

Sorry about the formatting. Let me know if you need more
info.

Separate but related question:

Are these two structures equivalent?

C code:

struct cstruct {

long l;

char c;

char *str;

int[30] ivals;

byte b;

lpvoid tmp;

char[20][30] string_array;

}



VB code:

Public Structure vbstruct

Public l As Integer

Public c As Char

Public str As StringBuilder

Public ivals As ArrayList

Public b As Byte

Public tmp As IntPtr

Public string_array As ArrayList

End Structure

Nope... This one is actually a little harder - because of the 2D
array. I it would most likely have to be done something like:

<StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)> _
Public Structure vbstruct
Public l As Integer

Public c As Char

' you can't marshal stringbuilder inside of
' a structure, have to use string
Public str As String

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=30)> _
Public ivals() As Integer

Public b As Byte

Public tmp As IntPtr

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=600)> _
Public string_array() As Char
End Sub

Basically, you would probably have to flatten the char array into a
single demension, and then chunk it up after the call manually. At
least, I haven't found a way to pass fixed size multidemensional arrays.
You could ask over on the interop group though, incase I missed it :)
 
D

Denis C

<snip>

<StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)
_
Public Structure vbstruct
Public l As Integer

Public c As Char

' you can't marshal stringbuilder inside of
' a structure, have to use string
Public str As String

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=30)> _
Public ivals() As Integer

Public b As Byte

Public tmp As IntPtr

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=600)> _
Public string_array() As Char
End Sub

Basically, you would probably have to flatten the char array into a
single demension, and then chunk it up after the call manually. At
least, I haven't found a way to pass fixed size multidemensional arrays.
You could ask over on the interop group though, incase I missed it :)

Thanks again, we're both new to VB and are unsure as to
what you can do with it. I should have mentioned that
this application will eventually be for Windows CE 4.2.
On there there is no MarshalAs and I believe the String
handling is different.

CHAR is a standard C char.

Is the passing of the structure in SetLongVal correct?

Thanks again,

Denis
 
T

Tom Shelton

<snip>

Ok, something to work with :). And I see a couple of things already...

And now that I know this is for the compact framework, things are a
little different. I'm not a great expert there, but I believe that the
calling convention is Cdecl there... So, you can disregard that.
Actually looking closer, I thinkg the problem is with your declaration
of SetLongVal....


<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Sub SetLongVal(ByRef temp As GetLongRTS)
End Sub

It returns void... So that is a Sub in VB - not a function, which
probably explains the variant error. If you still have troubles, then
you may want to post your questions to the framework interop group. I
know a bit about using marshalling in the framework, but I don't have
any experience at all with the compact framework. You might have better
luck over there if this doesn't solve it.
 
D

Denis C

-----Original Message-----
And now that I know this is for the compact framework, things are a
little different. I'm not a great expert there, but I believe that the
calling convention is Cdecl there... So, you can disregard that.
Actually looking closer, I thinkg the problem is with your declaration
of SetLongVal....



<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Sub SetLongVal(ByRef temp As GetLongRTS)
End Sub

It returns void... So that is a Sub in VB - not a function, which
probably explains the variant error. If you still have troubles, then
you may want to post your questions to the framework interop group. I
know a bit about using marshalling in the framework, but I don't have
any experience at all with the compact framework. You might have better
luck over there if this doesn't solve it.

Thanks again for all the help. I guess the main thing we
were looking for was passing pointers into a C library
from VB. We read that Structures in VB could only be
passed by value and were unsure how to proceed. Your
help is very much appreciated.

Denis
 
T

Tom Shelton

-----Original Message-----
Thanks again for all the help. I guess the main thing we
were looking for was passing pointers into a C library
from VB. We read that Structures in VB could only be
passed by value and were unsure how to proceed. Your
help is very much appreciated.

Denis

Actually, it's the other way around... Structures can only be passed
ByRef. So, you always get a pointer :)

Someday, I'm going to have to actually play with the compact
framework...
 
B

Bryan G

-----Original Message-----
And now that I know this is for the compact framework, things are a
little different. I'm not a great expert there, but I believe that the
calling convention is Cdecl there... So, you can disregard that.
Actually looking closer, I thinkg the problem is with your declaration
of SetLongVal....



<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Sub SetLongVal(ByRef temp As GetLongRTS)
End Sub

It returns void... So that is a Sub in VB - not a function, which
probably explains the variant error. If you still have troubles, then
you may want to post your questions to the framework interop group. I
know a bit about using marshalling in the framework, but I don't have
any experience at all with the compact framework. You might have better
luck over there if this doesn't solve it.

Thanks for all the help Tom, you've already saved us a lot
of grief and headaches :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top