473,704 Members | 2,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another C# marshaling question

Hi again,

I created a thread some days ago, while I was trying to access a C++ DLL
using my C# program. First of all, I want to thanks all the guys who helped
me. But I still have a problem... My C++ DLL has a function which has the
following prototype:

long FF_Function(voi d* inInstance, char* inDate, FF_Struct**
outStructure, long* outNumber);

The structure has the following prototype:

struct FF_Struct
{
long m_Index;
char m_ID[FF_ID_LEN];
char m_Date[FF_DATETIME_LEN];
};
I know that the function should return me an array of FF_Struct, and
that the parameter outNumber has the number of elements stored in the
FF_Struct array. Now, I'm tring to translate it in C#, and here is what I
did (which does not work, of course):
public class FF_AO_OrderList
{
public int index;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_ID_LEN)]
public string numeroCommande;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_DATETIME_LEN )]
public string dateCommande;
}
[DllImport("FF_m yDLL.dll")]
public static extern int FF_Function
(
IntPtr inInstance,
[MarshalAs(Unman agedType.LPTStr )]
string date,
out FF_Struct[] outStructure,
out int outNumber
);
I can access the function, but it returns me an error message ("The
parameters specified for the interface are incorrect."). I don't know how to
deal with this structure and its double pointer. Even in unsafe mode (then I
use a FF_Struct** parameter), I have the same problem.
Does anyone knows what I'm missing ? I'd greatly appreciate some help,
as I'm completely out of ideas... Thanks !!!!

Laurent
Nov 28 '05 #1
2 1957
Laurent,

You are going to have to manually marshal the structure. Your structure
declaration needs to be changed:

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Ansi)]
public class FF_Struct
{
public int index;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_ID_LEN)]
public string numeroCommande;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_DATETIME_LEN )]
public string dateCommande;
}

Declaring as a class will tell the CLR that it can re-arrange the order
of the fields as it wishes, which is not what you want in this case.

You will also have to change your function declaration to:

[DllImport("FF_m yDLL.dll")]
public static extern int FF_Function(
IntPtr inInstance,
[MarshalAs(Unman agedType.LPTStr )]
string date,
ref IntPtr outStructure,
ref int outNumber
);

Then, what you have to do on return is marshal the array, like so:

// Allocate the array.
FF_Struct[] outStructs = new FF_Struct[outNumber];

// Cycle through unmanaged memory, and marshal.
for (int index = 0; index < outNumber; index++)
{
// Get the pointer of the location in memory.
IntPtr elementPointer = new IntPtr(outStruc ture.ToInt64() + (index *
Marshal.SizeOf( typeof(FF_Struc t))));

// Now marshal that value.
outStructs[index] = Marshal.PtrToSt ructure(element Pointer,
typeof(FF_Struc t));
}

The only problem here is that you have to figure out how to release the
memory that was allocated for the structure. The function is allocating it,
but you aren't handling that in this situation.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Laurent" <mo********@fre e.fr> wrote in message
news:u2******** ********@tk2msf tngp13.phx.gbl. ..
Hi again,

I created a thread some days ago, while I was trying to access a C++
DLL using my C# program. First of all, I want to thanks all the guys who
helped me. But I still have a problem... My C++ DLL has a function which
has the following prototype:

long FF_Function(voi d* inInstance, char* inDate, FF_Struct**
outStructure, long* outNumber);

The structure has the following prototype:

struct FF_Struct
{
long m_Index;
char m_ID[FF_ID_LEN];
char m_Date[FF_DATETIME_LEN];
};
I know that the function should return me an array of FF_Struct, and
that the parameter outNumber has the number of elements stored in the
FF_Struct array. Now, I'm tring to translate it in C#, and here is what I
did (which does not work, of course):
public class FF_AO_OrderList
{
public int index;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_ID_LEN)]
public string numeroCommande;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_DATETIME_LEN )]
public string dateCommande;
}
[DllImport("FF_m yDLL.dll")]
public static extern int FF_Function
(
IntPtr inInstance,
[MarshalAs(Unman agedType.LPTStr )]
string date,
out FF_Struct[] outStructure,
out int outNumber
);
I can access the function, but it returns me an error message ("The
parameters specified for the interface are incorrect."). I don't know how
to deal with this structure and its double pointer. Even in unsafe mode
(then I use a FF_Struct** parameter), I have the same problem.
Does anyone knows what I'm missing ? I'd greatly appreciate some help,
as I'm completely out of ideas... Thanks !!!!

Laurent

Nov 28 '05 #2
Hi Nicholas,
Thank you for your answer, it works !!! I can access my function and the
returned value is as expected.

But I still have a little problem... this part of code doesn't work "as
it":

for (int index = 0; index < outNumber; index++)
{
// Get the pointer of the location in memory.
IntPtr elementPointer = new IntPtr(outStruc ture.ToInt64() + (index *
Marshal.SizeOf( typeof(FF_Struc t))));

// Now marshal that value.
outStructs[index] = Marshal.PtrToSt ructure(element Pointer,
typeof(FF_Struc t));
}
In fact, the first line is OK and the others have not been read
correctly. When I debug the code, Marshal.SizeOf( typeof(FF_Struc t)) = 32. I
wrote the part of memory I have read into a binary file and examinated it
with Hex Editor. The size of my structure is 29. When I manually put this
value into your code, it works perfectly.

I think something is wrong with my FF_Struct class definition, but I
don't know why. I'm sure the size of the char arrays is OK. Prehaps the
problem comes from the '\0' terminated string ??? Do you have any clue ?

Thanks anyway for your help, you taught me a lot of stuffs today ! :)

Laurent

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> a écrit
dans le message de news: O7************* *@tk2msftngp13. phx.gbl...
Laurent,

You are going to have to manually marshal the structure. Your
structure declaration needs to be changed:

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Ansi)]
public class FF_Struct
{
public int index;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_ID_LEN)]
public string numeroCommande;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_DATETIME_LEN )]
public string dateCommande;
}

Declaring as a class will tell the CLR that it can re-arrange the order
of the fields as it wishes, which is not what you want in this case.

You will also have to change your function declaration to:

[DllImport("FF_m yDLL.dll")]
public static extern int FF_Function(
IntPtr inInstance,
[MarshalAs(Unman agedType.LPTStr )]
string date,
ref IntPtr outStructure,
ref int outNumber
);

Then, what you have to do on return is marshal the array, like so:

// Allocate the array.
FF_Struct[] outStructs = new FF_Struct[outNumber];

// Cycle through unmanaged memory, and marshal.
for (int index = 0; index < outNumber; index++)
{
// Get the pointer of the location in memory.
IntPtr elementPointer = new IntPtr(outStruc ture.ToInt64() + (index *
Marshal.SizeOf( typeof(FF_Struc t))));

// Now marshal that value.
outStructs[index] = Marshal.PtrToSt ructure(element Pointer,
typeof(FF_Struc t));
}

The only problem here is that you have to figure out how to release the
memory that was allocated for the structure. The function is allocating
it, but you aren't handling that in this situation.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Laurent" <mo********@fre e.fr> wrote in message
news:u2******** ********@tk2msf tngp13.phx.gbl. ..
Hi again,

I created a thread some days ago, while I was trying to access a C++
DLL using my C# program. First of all, I want to thanks all the guys who
helped me. But I still have a problem... My C++ DLL has a function which
has the following prototype:

long FF_Function(voi d* inInstance, char* inDate, FF_Struct**
outStructure, long* outNumber);

The structure has the following prototype:

struct FF_Struct
{
long m_Index;
char m_ID[FF_ID_LEN];
char m_Date[FF_DATETIME_LEN];
};
I know that the function should return me an array of FF_Struct, and
that the parameter outNumber has the number of elements stored in the
FF_Struct array. Now, I'm tring to translate it in C#, and here is what I
did (which does not work, of course):
public class FF_AO_OrderList
{
public int index;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_ID_LEN)]
public string numeroCommande;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = FF_DATETIME_LEN )]
public string dateCommande;
}
[DllImport("FF_m yDLL.dll")]
public static extern int FF_Function
(
IntPtr inInstance,
[MarshalAs(Unman agedType.LPTStr )]
string date,
out FF_Struct[] outStructure,
out int outNumber
);
I can access the function, but it returns me an error message ("The
parameters specified for the interface are incorrect."). I don't know how
to deal with this structure and its double pointer. Even in unsafe mode
(then I use a FF_Struct** parameter), I have the same problem.
Does anyone knows what I'm missing ? I'd greatly appreciate some help,
as I'm completely out of ideas... Thanks !!!!

Laurent


Nov 29 '05 #3

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

Similar topics

1
7255
by: C. N. Sridhar | last post by:
Hi, I'm writing a wrapper to a win32 dll in C#. I need to call a method in DLL which has a Variant type reference parameter. How to marshal variant type from win32 (unmanaged code) to C# (managed code)? I tried using Marshal.GetObjectForNativeVariant(), but of no use.
0
1886
by: Ash | last post by:
Hi, Has anyone ever tried using "COM marshaling" for cross- process communication? In particular, I am interested in cross-process marshaling between Managed Server and Unmanaged Client. There seems to be very little documentation/examples on this. I have a "Service" that needs to have a COM
3
2663
by: Nikolay Petrov | last post by:
Guys, please help. I am trying to make this work from at least 4 months. I am new to programming and some things are difficult to me, but I really need to make my project work. I can't find anything helpfull in internet for 4 months ;-( . Please someone who is more expirenced help me. I am trying to use the Terminal Server APIs There is as an API function WTSEnumerateSessions, which returns a WTS_SESSION_INFO custom structure n times....
3
3640
by: Rudy Velthuis | last post by:
Hello, Does anyone know how to create a struct that will marshal to the following C++ struct A, containing an array of the user defined String10 type: struct String10 { char SLen; char S;
0
1470
by: Jeff | last post by:
Hi guys Mattias, thanx for answering my last question Well, I'm struggling with marshaling a struct that has **ptr to an array of arrays of struct. Why? I'm stuck with it 1. I need to know how to get to the contents of "dumberstruct" contained within "dumbstruct" (i.e. dumbstruct.dstruct) 2. How to get to dumbstruct.tag_field_value and dumbstruct.field_value Code follows
2
3952
by: mirek | last post by:
Hi, I'm trying to import my old code to the .NET using managed wrappers. I've read "Managed Extensions for C++ Migration Guide" document and was trying to do what it stated in it. For example if my function takes mfc's CString I was converting it this way: char __pin* szUser = static_cast<char*>(Marshal::StringToHGlobalAnsi(sUser).ToPointer()); and pass szUser to the function.
1
1055
by: Laurent | last post by:
Hi again, I need your help again for my C# programm calling a C++ DLL. This is driving me crazy... I have a function defined like this: long FF_AO_GetOrderInfo ( void* inInstance, char* inOrderID,
25
2505
by: MuZZy | last post by:
Hi, I'm currently rewriting some functionality which was using multithredaing for retrieving datasets from database and updating a grid control. I found that the grids (Infragistics UltraGrid, though it doesn't matter) were updated (i.e. grid.DataSource = dsDataSet;) and used for different purposes in the worker thread, so now i'm wrapping all those calls to grid's methods thru invoking which is a pain in the a$$ considering number of...
1
2673
by: Bill Medland | last post by:
I am trying to do some P/Invoke custom marshaling and am looking for a little help on custom marshaling a value type. I am working based on Kate Gregory's "Arranging Custom Marshaling With P/Invoke", which is unfortunately in C++ with managed extensions; I am working in C#. As with Kate's example I want to marshal from a System.DateTime to a proprietary format. However I cannot figure out how to tell the system to box the DateTime. ...
0
1874
by: jpogorman | last post by:
Hello, I am trying to get c# custom marshaling working in a particular scenario but it does not appear to be working or not jumping into my marshaling class when I try to debug it. I am try to implement a performance improvement in the transfer of thousands of objects (ClientInfoDescriptor) over a remote call. The objects all have a model object (IDataDrivenModel) to describe them but there may only two or three model objects in total. These...
0
8764
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8677
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
9266
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...
1
9020
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
7879
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
6603
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
5926
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3130
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
3
2074
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.