473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Passing Pointers to Structs

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.MarshalDirectiveEx cept
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.
Nov 20 '05 #1
8 1776
On 2003-11-21, Bryan G <an*******@discussions.microsoft.com> wrote:
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.MarshalDirectiveEx cept
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.

--
Tom Shelton
[MVP - Visual Basic]
Nov 20 '05 #2
-----Original Message-----
On 2003-11-21, Bryan G <an*******@discussions.microsoft.com> wrote:
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.MarshalDirectiveEx cep
t 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 theC definitions of the structure and function and seeing your VBdeclarations. If you could post that information, I or someone else, Iam sure, would be happy to help you.

--
Tom Shelton
[MVP - Visual Basic]
.

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
Nov 20 '05 #3
On 2003-11-21, Denis C <an*******@discussions.microsoft.com> wrote:
-----Original Message-----
On 2003-11-21, Bryan G<an*******@discussions.microsoft.com> wrote:


<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 :)

--
Tom Shelton
[MVP - Visual Basic]
Nov 20 '05 #4
<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 asingle demension, and then chunk it up after the call manually. Atleast, 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 :)
--
Tom Shelton
[MVP - Visual Basic]
.


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
Nov 20 '05 #5
On 2003-11-21, Tom Shelton <to*@lsnfcdm.com> wrote:
On 2003-11-21, Denis C <an*******@discussions.microsoft.com> wrote:
-----Original Message-----
On 2003-11-21, Bryan G

<an*******@discussions.microsoft.com> wrote:


<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....
DBACCESSDLL_API void __cdecl SetLongVal(GetLongRT temp){
temp->sval = 'b';
}

<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.

--
Tom Shelton
[MVP - Visual Basic]
Nov 20 '05 #6
-----Original Message-----
On 2003-11-21, Tom Shelton <to*@lsnfcdm.com> wrote:
On 2003-11-21, Denis C <an*******@discussions.microsoft.com> wrote:

-----Original Message-----
On 2003-11-21, Bryan G
<an*******@discussions.microsoft.com> wrote:
<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 alittle different. I'm not a great expert there, but I believe that thecalling convention is Cdecl there... So, you can disregard that.Actually looking closer, I thinkg the problem is with your declarationof SetLongVal....
DBACCESSDLL_API void __cdecl SetLongVal(GetLongRT temp){ temp->sval = 'b';
}


<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, whichprobably explains the variant error. If you still have troubles, thenyou may want to post your questions to the framework interop group. Iknow a bit about using marshalling in the framework, but I don't haveany experience at all with the compact framework. You might have betterluck over there if this doesn't solve it.

--
Tom Shelton
[MVP - Visual Basic]
.


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
Nov 20 '05 #7
On 2003-11-22, Denis C <an*******@discussions.microsoft.com> wrote:
-----Original Message-----
On 2003-11-21, Tom Shelton <to*@lsnfcdm.com> wrote:
On 2003-11-21, Denis C<an*******@discussions.microsoft.com> wrote:
>-----Original Message-----
>On 2003-11-21, Bryan G
<an*******@discussions.microsoft.com> wrote:

<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....
DBACCESSDLL_API void __cdecl SetLongVal(GetLongRT temp){ temp->sval = 'b';
}

<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.

--
Tom Shelton
[MVP - Visual Basic]
.


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...

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #8
-----Original Message-----
On 2003-11-21, Tom Shelton <to*@lsnfcdm.com> wrote:
On 2003-11-21, Denis C <an*******@discussions.microsoft.com> wrote:

-----Original Message-----
On 2003-11-21, Bryan G
<an*******@discussions.microsoft.com> wrote:
<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 alittle different. I'm not a great expert there, but I believe that thecalling convention is Cdecl there... So, you can disregard that.Actually looking closer, I thinkg the problem is with your declarationof SetLongVal....
DBACCESSDLL_API void __cdecl SetLongVal(GetLongRT temp) { temp->sval = 'b';
}


<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, whichprobably explains the variant error. If you still have troubles, thenyou may want to post your questions to the framework interop group. Iknow a bit about using marshalling in the framework, but I don't haveany experience at all with the compact framework. You might have betterluck over there if this doesn't solve it.

--
Tom Shelton
[MVP - Visual Basic]
.


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Seung-Uk Oh | last post by:
Hi, everyone! We are developing an application using both C and C++. We have defined a structure in a C program as follows: typedef struct node { struct node *next; int value; }List;
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
3
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
2
by: WTH | last post by:
I have a C# webservice that returns an array of struct data back to a calling client. It works fine when tested via the ASMX page; however, I don't know how to make the call to this particular...
3
by: sd2004 | last post by:
I am still learning, could someone show/explain to me how to fix the error. I can see it is being wrong but do not know how to fix. could you also recommend a book that I can ref. to ?...
17
by: Johan Tibell | last post by:
Could someone outline the pros and cons of typedefing pointers to structs like this? typedef struct exp_ { int val; struct exp_ *child; } *exp; (This is straight from memory so it might not...
9
by: nolonger | last post by:
Sample code:- typedef struct __Y { int a; char b; } Y; Y my_struct; Y *my_ptr_struct;
0
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.