473,769 Members | 6,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cha r* 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(La youtKind.Sequen tial)]
internal class IrData
{
public ushort IrFormat;
public ushort IrCodeLength;
public byte[] IrCodeData;
}

[DllImport("Some Dll.dll")]
private static extern short ConvertData(str ing strData, IrData
data);

Thx in Advance.

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

Function in C DLL
----------------------------
short ConvertData(cha r* 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(La youtKind.Sequen tial)]
public struct IrData
{
public ushort IrFormat;
public ushort IrCodeLength;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 256]
public byte[] IrCodeData;
}

[DllImport("Some DLL.dll")]
private static extern short ConvertData(str ing strData, ref
IrData data);




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

Function in C DLL
----------------------------
short ConvertData(cha r* 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(La youtKind.Sequen tial)]
public struct IrData
{
public ushort IrFormat;
public ushort IrCodeLength;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 256]
public byte[] IrCodeData;
}

[DllImport("Some DLL.dll")]
private static extern short ConvertData(str ing 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
9427
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 declaration: extern "C" struct X; that is then defined as:
4
5984
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
14506
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 program (UNIX) as an array of "struct point" where struct point //C structure { int Time; //32 bits
10
2745
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
28469
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
40471
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 familiar with "typedef int NUMBER", but how does it work with structures. Tilak
15
2681
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 then adds values to "jd" 3.) in the end it prints values of "jd".
0
1709
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 "struct.py" not being able to find the module "_struct". struct.py is located under /usr/local/lib/python-2.5/, and there is a _struct.o (no _struct.py anywhere) located under /usr/local/lib/ python-2.5/lib-dynload.
8
2170
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 (where there is an
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.