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

PInvoke "Struct" Problem

Structure in C DLL
----------------------------
typedef struct IrData
{
unsigned short uiFormat;
unsigned short uiLength;
unsigned char* pchData;
} tagIrData;

Function in C DLL
----------------------------
short ConvertData(char* pchString, tagIrData* ptIrCode); ---This
function fills the allocated structure with the respective field
values.

How should we write the PInvoke for this ? I have tried the method
below (with both struct & class) but my structure elements go for a
toss. Please correct me.

[StructLayout(LayoutKind.Sequential)]
internal class IrData
{
public ushort IrFormat;
public ushort IrCodeLength;
public byte[] IrCodeData;
}

[DllImport("SomeDll.dll")]
private static extern short ConvertData(string strData, IrData
data);

Thx in Advance.

Jan 23 '07 #1
3 4316
<an***************@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Structure in C DLL
----------------------------
typedef struct IrData
{
unsigned short uiFormat;
unsigned short uiLength;
unsigned char* pchData;
} tagIrData;

Function in C DLL
----------------------------
short ConvertData(char* pchString, tagIrData* ptIrCode); ---This
function fills the allocated structure with the respective field
values.

How should we write the PInvoke for this ? I have tried the method
below (with both struct & class)
You can use either. If using a class pass by value and it gets passed by
reference, if using a struct you need to pass by reference. I almost always
use class because you can add functions to the class etc. Once that is
working the only problem will be the char* but I think that will work as it
is. The only other thing to check would be the layout of your structure, use
the FieldOffset attribute to make sure it has the exact layout you require
(although I don't think this will be required).

Michael
Jan 23 '07 #2
I did this, but no luck !

[StructLayout(LayoutKind.Sequential)]
public struct IrData
{
public ushort IrFormat;
public ushort IrCodeLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256]
public byte[] IrCodeData;
}

[DllImport("SomeDLL.dll")]
private static extern short ConvertData(string strData, ref
IrData data);




Michael C wrote:
<an***************@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Structure in C DLL
----------------------------
typedef struct IrData
{
unsigned short uiFormat;
unsigned short uiLength;
unsigned char* pchData;
} tagIrData;

Function in C DLL
----------------------------
short ConvertData(char* pchString, tagIrData* ptIrCode); ---This
function fills the allocated structure with the respective field
values.

How should we write the PInvoke for this ? I have tried the method
below (with both struct & class)

You can use either. If using a class pass by value and it gets passed by
reference, if using a struct you need to pass by reference. I almost always
use class because you can add functions to the class etc. Once that is
working the only problem will be the char* but I think that will work as it
is. The only other thing to check would be the layout of your structure, use
the FieldOffset attribute to make sure it has the exact layout you require
(although I don't think this will be required).

Michael
Jan 23 '07 #3
igd
Replace 'struct' with 'class' and initialize at least the array (public
byte[] lrCodeData = new byte[256];)

an***************@gmail.com schrieb:
I did this, but no luck !

[StructLayout(LayoutKind.Sequential)]
public struct IrData
{
public ushort IrFormat;
public ushort IrCodeLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256]
public byte[] IrCodeData;
}

[DllImport("SomeDLL.dll")]
private static extern short ConvertData(string strData, ref
IrData data);
Jan 23 '07 #4

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

Similar topics

11
by: Daniele Benegiamo | last post by:
Is the following program standard-compliant? I've compiled it with some compilers and no errors or warnings are produced, but this is not a demonstration. The "incriminated" part is the...
4
by: Yair | last post by:
Hi, I need to shift the bits of a float type. In order to do so, I declared the following struct: private struct unionIntFloatType { public float m_asFloat;
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
10
by: Pantokrator | last post by:
Hi, Is there any way to "overload" a struct? e.g. having already struct stA1 { int i_ID; int i_Type; };
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
8
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
0
by: Keith | last post by:
Hello, I am trying to create exectuables on inux using "pyinstaller". I am using pyinstaller-1.3, RHEL 4.4, Python 2.5. The executables fail to run. The problem returned is pertaining to...
8
by: anon.asdf | last post by:
Hi! OK, lets try "array-copy": { char arrayA; arrayA = (char){1, 2, 3}; } it does *not* work since we're trying to make a fixed array-pointer arrayA, point to another location/address...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.