473,698 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Yet another marshalling question

Hello All,
I am very new to C# since my job consist to design/develop CE drivers but a
client wants to use my C API into a .NET envrironment.

I have 2 functions I would like to get help with.

1- BOOL WriteDataSyncCa rd (unsigned short Address, unsigned char* pBuffer,
unsigned char* Length);
pBuffer is allocated outside the function and will not modified
Length is a 1 byte length value and can modified in the function with the
actual written number of bytes.

2- BOOL ReadDataSyncCar d (unsigned short Address, unsigned char* pBuffer,
unsigned char* Length);
pBuffer is allocated outside the function and will be updated with the data
read from the card
Length is a 1 byte length value and can modified in the function with the
actual read number of bytes.

I have look aroud and found a few informations on what to be done. Here is
what I have done for now.

[DllImport("Sync ContactCardAPI. dll")]
private static extern int WriteDataSyncCa rd(Int16 Address, byte[] pBuffer,
IntPtr Length);

public static int WriteData(Int16 Address, byte[] pBuffer, IntPtr Length)
{
//Do I need to do something with the parameters here since they are only
a pass-throught??
int Status = WriteDataSyncCa rd(Address, pBuffer, Length);
return Status;
}

[DllImport("Sync ContactCardAPI. dll")]
private static extern int ReadDataSyncCar d(Int16 Address, IntPtr pBuffer,
IntPtr Length);

public static int ReadData(Int16 Address, out byte[] pBuffer, IntPtr Length)
{
int Offset = 0;
byte BufferSize = //How do I get the 1 byte value pointed by Length??;

IntPtr Data = MemoryManager.A llocHeap((int)B ufferSize);

int Status = ReadDataSyncCar d(Address, Data, Length);

//Getting actual read size
BufferSize = //How do I get the 1 byte value pointed by Length??;

pBuffer = new byte[io_ProfileSize];
for (uint i = 0; i < BufferSize; i++)
{
pBuffer[i] = Marshal.ReadByt e(Data, Offset);
Offset++;
}

MemoryManager.F reeHeap(Data);

return Status;
}

Thank you very much!

Keaven
Jan 9 '08 #1
1 1388
Keaven,

You don't need to declare the Length parameters as IntPtrs and then
allocate memory for them. Just declare them as ref byte and then pass them
as such and it will work just fine.

Also, are the unsigned chars strings? If it is, you can declare them as
strings, and pass them directly, instead of doing the conversion yourself.
Just make sure to attach the MarshalAs attribute, setting the UnmanagedType
to UnmanagedType.L PStr.

Finally, if you are using unsigned shorts, then you should use the
ushort type. Int16 is signed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Keaven Pineau" <ke****@no.spam .comwrote in message
news:Oy******** ******@TK2MSFTN GP05.phx.gbl...
Hello All,
I am very new to C# since my job consist to design/develop CE drivers but
a client wants to use my C API into a .NET envrironment.

I have 2 functions I would like to get help with.

1- BOOL WriteDataSyncCa rd (unsigned short Address, unsigned char*
pBuffer, unsigned char* Length);
pBuffer is allocated outside the function and will not modified
Length is a 1 byte length value and can modified in the function with the
actual written number of bytes.

2- BOOL ReadDataSyncCar d (unsigned short Address, unsigned char* pBuffer,
unsigned char* Length);
pBuffer is allocated outside the function and will be updated with the
data read from the card
Length is a 1 byte length value and can modified in the function with the
actual read number of bytes.

I have look aroud and found a few informations on what to be done. Here
is what I have done for now.

[DllImport("Sync ContactCardAPI. dll")]
private static extern int WriteDataSyncCa rd(Int16 Address, byte[] pBuffer,
IntPtr Length);

public static int WriteData(Int16 Address, byte[] pBuffer, IntPtr Length)
{
//Do I need to do something with the parameters here since they are
only a pass-throught??
int Status = WriteDataSyncCa rd(Address, pBuffer, Length);
return Status;
}

[DllImport("Sync ContactCardAPI. dll")]
private static extern int ReadDataSyncCar d(Int16 Address, IntPtr pBuffer,
IntPtr Length);

public static int ReadData(Int16 Address, out byte[] pBuffer, IntPtr
Length)
{
int Offset = 0;
byte BufferSize = //How do I get the 1 byte value pointed by Length??;

IntPtr Data = MemoryManager.A llocHeap((int)B ufferSize);

int Status = ReadDataSyncCar d(Address, Data, Length);

//Getting actual read size
BufferSize = //How do I get the 1 byte value pointed by Length??;

pBuffer = new byte[io_ProfileSize];
for (uint i = 0; i < BufferSize; i++)
{
pBuffer[i] = Marshal.ReadByt e(Data, Offset);
Offset++;
}

MemoryManager.F reeHeap(Data);

return Status;
}

Thank you very much!

Keaven

Jan 9 '08 #2

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

Similar topics

3
2834
by: PHil Coveney | last post by:
Hello, I am having difficulty marshalling structures when calling DeviceIoControl. I am importing this Win32 function as static extern int DeviceIoControl (int hDevice, int dwIoControlCode, ref int lpInBuffer,
4
2864
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id; double s_id;
1
1101
by: MuZZy | last post by:
Hello, I am probably facing a sort of language barrier, but i can't really get what 'marshalling' is in terms of .NET Is it a kind of type casting? And when do you use it? Thank you, Andrey
2
5786
by: Rookie | last post by:
Hi, I had a question on DllImport. On importing a function from a VC++ dll using DllImport (to a C# program), the function argument data types and the return types may be of a type that is not supported by C#. In this case if I am not mistaken the system performs default marshalling - matching the data types to its most similar equivalent in C#. Is this right? Also, I presume this can be overridden by using MarshalAs. Is this
2
1276
by: BartMan | last post by:
Greetings, When working with managed c++, do you have to do anything special when going from simple types from managed to unmanaged and vice versa. Or is marshalling handled automatically for you? I have seen from several posts that you have to do special conversions for strings, but what about the other types such as int, long,ect. In my case I want to save it as a member variable from a managed class into an unmanaged class's.
1
276
by: GianPiero Andreis | last post by:
Hello All, let me pose a simple question about combobox and the CB_INSERTSTRING message. Suppose for instance that I already have a combobox handle, how can I declare and use the SendMessage function just for insert a new item into the combobox ? Of course, the combo box DO NOT belong to my process, so I can't use the standard properties of that class. In fact, I need to "put" a new entry in the combo box of IExplorer. EnumWindows get...
10
2542
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm actually deserializing a SOAP document into an object and parsing out the fields, but that's fairly immaterial to this
2
2487
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall GetChildren(GetChildrenParm *, Results *);
5
2884
by: PickwickBob3 | last post by:
I am trying to obtain a list of HID devices and am trying to use UINT GetRawInputDeviceList( PRAWINPUTDEVICELIST pRawInputDeviceList, PUINT puiNumDevices, UINT cbSize); a USER32.dll. but the function fails. Here is the code. I am new to Marshalling. Can anyone suggest my error and why? I am using C# with Visual Studio 2003 with 1.1 .NET Code: /*typedef struct tagRAWINPUTDEVICELIST { HANDLE hDevice; DWORD dwType;
2
3345
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I need to pass an array of RADIO_INFO2 structures to be filled by a function in the DLL. This is how the structure is defined in the C++ example that comes with the DLL:
0
8603
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
9023
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...
1
8893
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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...
0
7721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
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
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.