473,395 Members | 1,972 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.

C Structure

Dan
Good Day All,

I have a C function I am trying to call from C# using DLL Import. The
definition for the function is:

int IAListNext(hIAClient, dwHandle, lpData, iMaxLen, lpdwLen)

What has me confused is the ldData parameter. It is of type char FAR *. In
the documentation (which was originall written for the C and VB 6 programmer)
if further defines the parameter as follows:

For C, a pointer to location where the function returns the information for
the
next item in the list.
For VB, a data structure containing an ID of type Long and a Name of type
String having sufficient size to hold all the values that will be returned
to the
list.

I have tired passing a StringBuilder but that did not work. I have also
tried passing, by ref, the following structure:

public struct IATaskListRecord
{

public int ID;
public string Name;

}

But that did not work either. I get a NullReference Exception.

Does anyone have any idea how I should deal with this. I am at a loss.

Thanks!

Dan
Nov 16 '05 #1
1 1175
Dan wrote:
Good Day All,

I have a C function I am trying to call from C# using DLL Import. The
definition for the function is:

int IAListNext(hIAClient, dwHandle, lpData, iMaxLen, lpdwLen)

<snip>

It depends on how this string is supposed to work.

If the string in the struct is a pointer to where the string is located,
this would do:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct SomeStruct
{
public Int32 ID;

[MarshalAs(UnmanagedType.LPStr)]
public StringBuilder Text;
}

however, when using it, you need to:

SomeStruct ss = new SomeStruct();
ss.Text = new StringBuilder(1000); // make 1000 big enough
IAListNext(..., ref ss, ...)

If, on the other hand, the string is defined as a character array in the
struct, this would do:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct SomeStruct
{
public Int32 ID;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1000)]
public StringBuilder Text;
}

Note the following:
CharSet=CharSet.Ansi <-- 8-bit characters
CharSet=CharSet.Unicode <-- Text is unicode
SizeConst=1000 <-- number of characters, not bytes

The definition of your IAListNext method would be:

[DllImport("Yourdll.dll")]
public static extern Int32 IAListNext(
IntPtr hIAClient,
Int32 dwHandle,
ref SomeStruct lpData,
Int32 iMaxLen,
Int32 lpdwLen);

The rules for String vs. StringBuilder is:

[in]: String
[out]: StringBuilder
[in/out]: StringBuilder

in --> pass data to method
out --> method changes data before returning

Hope this helps.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #2

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

Similar topics

2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
4
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
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
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
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:
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
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
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,...

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.