473,386 Members | 1,752 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,386 software developers and data experts.

PInvoke and structs question question

Hello:

I'm trying to execute a function of a unmanaged dll using PInvoke, i
have definied the function as:

[DllImport(FbClient.DllPath)]
public static extern int isc_dsql_prepare(
int[] status_vector,
ref int trans_handle,
ref int stmt_handle,
short length,
string statement,
short dialect,
ref XSQLDA xsqlda);

the XSQLDA structure is defined as:

[StructLayout(LayoutKind.Sequential)]
public struct XSQLDA
{
public short version;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=8)]
public string sqldaid;
public int sqldabc;
public short sqln;
public short sqld;
public XSQLVAR[] sqlvar;
}

[StructLayout(LayoutKind.Sequential)]
public struct XSQLVAR
{
public short sqlType;
public short sqlScale;
public short sqlSubType;
public short sqlLen;
public byte[] sqlData;
public short sqlInd;
public short sqlNameLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string sqlName;
public short relNameLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string relName;
public short ownerNameLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string ownerName;
public short aliasNameLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string aliasName;
}

But i get always a TypeLoadException when i exec it, the problem seems
to be with :

public XSQLVAR[] sqlvar;

Removing this line i can execute the function but i need it :)

Any idea on how to solve this ??

Thanks in advance

--
Best regards

Carlos Guzmán Álvarez
Vigo-Spain

Nov 15 '05 #1
5 2497
On 2003-11-30, Carlos Guzmán Álvarez <ca*******@telefonica.net> wrote:

<snip>

public XSQLVAR[] sqlvar;


The marshaller does not support arrays of structs inside of structs.
You will want to change this to an IntPtr - since it is a dynmaic sized
array. Then use the members of the marshal class (specifically, I would
look at the Marshal.PtrToStructure and Marshal.SizeOf).

--
Tom Shelton
MVP [Visual Basic]
Nov 15 '05 #2
Hello:
The marshaller does not support arrays of structs inside of structs.
You will want to change this to an IntPtr - since it is a dynmaic sized
array. Then use the members of the marshal class (specifically, I would
look at the Marshal.PtrToStructure and Marshal.SizeOf).


Thanks, i'm starting to see PInvoke with C# and have a lot of doubts on
how it works :)

One question, i get the same error if i define it as:

public XSQLVAR sqlvar;

It's not supported too ??

And another question, i'm trying to use it with IntPtr as you suggested,
to initialize the IntPtr i'm making:

XSQLDA xsqlda = new XSQLDA();
xsqlda.version = 1;
xsqlda.sqln = 1;
xsqlda.sqlvar = new XSQLVAR[1];
xsqlda.sqlvar[0].sqlData = new byte[0];

int size = Marshal.SizeOf(xsqlda);
IntPtr sqlda = Marshal.AllocCoTaskMem(size);
Marshal.StructureToPtr(xsqlda, sqlda, true);

But i get an ArgumentException on:

int size = Marshal.SizeOf(xsqlda);
Any idea on how to solve it ??
Thanks in advance.
--
Best regards

Carlos Guzmán Álvarez
Vigo-Spain

Nov 15 '05 #3
Carlos,
But i get an ArgumentException on:


What does the exception message say? I'm guessing it's caused by the
byte[] in XSQLVAR. If it's supposed to be a fixed length array, you
must use the MarshalAs attribute to indicate its size. If it's a
pointer to a variable length array, change its type to an IntPtr or
byte*.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4
Hello:
What does the exception message say? I'm guessing it's caused by the
byte[] in XSQLVAR.
You are right :)
If it's supposed to be a fixed length array, you
must use the MarshalAs attribute to indicate its size. If it's a
pointer to a variable length array, change its type to an IntPtr or
byte*.


it's dynamic array, for give a little more or information, the original
C structs are:

typedef struct
{
short sqltype;
short sqlscale;
short sqlsubtype;
short sqllen;
char * sqldata;
short * sqlind;
short sqlname_length;
char sqlname[32];
short relname_length;
char relname[32];
short ownname_length;
char ownname[32];
short aliasname_length;
char aliasname[32];
} XSQLVAR;

typedef struct
{
short version;
char sqldaid[8];
ISC_LONG sqldabc;
short sqln;
short sqld;
XSQLVAR sqlvar[1];
} XSQLDA;
And the original C function definition is:

ISC_STATUS isc_dsql_describe(
ISC_STATUS *status_vector,
isc_stmt_handle *stmt_handle,
unsigned short da_version,
XSQLDA *xsqlda);

And a little sample of use:

ISC_STATUS status_vector[20];
XSQLDA *osqlda;

....

osqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(n);
osqlda->sqln = n;
osqlda->version = SQLDA_VERSION1;
isc_dsql_describe(
status_vector,
&stmt_handle,
1,
osqlda);

....
Can be this handled using PInvoke ??
The C# PInvoke definition i have now is:

[DllImport(FbClient.DllPath)]
public static extern int isc_dsql_describe(
int[] status_vector,
ref int stmt_handle,
short da_version,
ref XSQLDA xsqlda);

And the C# structures i'm using now are:

[StructLayout(LayoutKind.Sequential)]
public struct XSQLVAR
{
public short sqlType;
public short sqlScale;
public short sqlSubType;
public short sqlLen;
public IntPtr sqlData;
public IntPtr sqlInd;
public short sqlNameLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string sqlName;
public short relNameLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string relName;
public short ownerNameLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string ownerName;
public short aliasNameLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string aliasName;
}

[StructLayout(LayoutKind.Sequential)]
public struct XSQLDA : IDisposable
{
public short version;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=8)]
public string sqldaid;
public int sqldabc;
public short sqln;
public short sqld;
public IntPtr sqlvar;
}
Can be this correct for work with PInvoke or not ??

Thanks in advance.


--
Best regards

Carlos Guzmán Álvarez
Vigo-Spain


Nov 15 '05 #5
Hello:
The marshaller does not support arrays of structs inside of structs.
You will want to change this to an IntPtr - since it is a dynmaic sized
array. Then use the members of the marshal class (specifically, I would
look at the Marshal.PtrToStructure and Marshal.SizeOf).


I think i have it working now, i need to more serious tests using
different functions that make use of these structures, but at least the
first tests seems to be working, thanks very much to all.



--
Best regards

Carlos Guzmán Álvarez
Vigo-Spain

Nov 15 '05 #6

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

Similar topics

11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
1
by: cppdev | last post by:
Hello, After reading a few articles, http://blogs.gotdotnet.com/cbrumme/PermaLink.aspx/e55664b4-6471-48b9-b360-f0fa27ab6cc0...
5
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. ...
2
by: steve | last post by:
Hi all, I want to understand more about how the pinvoke pinning process works. I'm writing some code that calls DeviceIoControl. DeviceIoControl provides a generic interface to device drivers....
3
by: Michael | last post by:
Hi all, I'm having trouble PInvoking a TCHAR within a struct. I'll paste the specific struct's API definition below. I've tried so many numerous variations. The main Win32 error I get is...
5
by: ravi.sathyam | last post by:
Hey, So say I have a sockaddr_in struct stored in a packet which I receive from my udp socket.....and it is stored within a certain offset into this packet (which is basically a char array)....
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
1
by: eastlands | last post by:
I need to use an unmanaged c++ dll which uses structs that contain callbacks and also functions. I have included the appropriate c++ definitials and my c# translations below. I first defined the...
5
by: =?Utf-8?B?SmVzc2ljYQ==?= | last post by:
Hello, I have a pInvoke question. This is the C function that is exported from one of the C dll, extern __declspec(dllexport) IM_RET_CODE ST_import (IM_MODE mode, char *filename,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
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
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
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,...
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...

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.