473,324 Members | 2,400 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,324 software developers and data experts.

Point to array in Csharp and native dll

I have a dll writtin in native C++ which provide such interface:

typedef struct _PARAM
{
LPWSTR swName;
DWORD dwOptions;
} PARAM,*PPARAM,*LPPARAM;

DLLEXPORT BOOL CALLBACK Function(LPPARAM pData);

Call as:

PARAM ParamsArray[]={
{L"Anna",202},
{L"John",324},
{L"Doe",4094},
{L"etc..",3003},
{NULL,0}
};
Function((PPARAM)&ParamsArray);

How to call this native dll from managed C# code?
How to easy init array of such params in C# and correctly send it to
dll function?

I created C# class
namespace test
{
class NativeDll
{
[StructLayout(LayoutKind.Sequential)]
public struct PARAM
{
[MarshalAs(UnmanagedType.LPStr)]
public String swName;
public uint dwOptions;
};

[DllImport("Easymovedll.dll", CallingConvention =
CallingConvention.Winapi,
CharSet=CharSet.Unicode,EntryPoint="_Function")]
public static extern bool Function(ref PARAM[] PPARAM);
}
}
Calling in C#:
using test;
NativeDll.PARAM[] ParamsArray=new NativeDll.PARAM[2];
PARAM[0].swName="john";
PARAM[0].dwOptions=2093;
PARAM[1].swName=null;
PARAM[1].dwOptions=0;
NativeDll.Function(ref ParamsArray);

But this is not give me correct array is I want. What I doing wrong?

Apr 24 '07 #1
3 1955
>How to easy init array of such params in C#

It would be slightly easier if you add a contructor to the PARAM
struct.

[DllImport("Easymovedll.dll", CallingConvention =
CallingConvention.Winapi,
CharSet=CharSet.Unicode,EntryPoint="_Function")]
public static extern bool Function(ref PARAM[] PPARAM);
^^^

Try it without the ref modifier.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 24 '07 #2
>
[DllImport("Easymovedll.dll", CallingConvention =
CallingConvention.Winapi,
CharSet=CharSet.Unicode,EntryPoint="_Function")]
public static extern bool Function(ref PARAM[] PPARAM);

^^^

Try it without the ref modifier.
Yes this is work but when other functions is called, it seems to be
array was destroyed, and it unpossible to use this array. How to send
permanent point to PARAM array, so it will be accessible when other
dll functions is called?

Apr 26 '07 #3
>Yes this is work but when other functions is called, it seems to be
array was destroyed, and it unpossible to use this array. How to send
permanent point to PARAM array, so it will be accessible when other
dll functions is called?
You have to keep the array pinned in memory as long as the native code
uses it. You can explicitly pin an object using the GCHandle type.

But since pinning for longer durations can hurt GC, it might be a
better idea to allocate a buffer from a native heap using for example
Marshal.AllocHGlobal and pass that to the DLL instead. You can then
retrieve the PARAM values from the buffer with Marshal.PtrToStructure.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 26 '07 #4

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

Similar topics

47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
1
by: Tamir Khason | last post by:
Somebody knows how to convert Bounds to Point ??? Thnx
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
8
by: Ian Stiles | last post by:
If you have "Show window contents while dragging" turned on (Right-click desktop, Appearance, Effects) then you get horrible flashing and flickering on a CSharp form when the form hosts a...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
6
by: Larry Serflaten | last post by:
I am trying to add 2 points together, and I am not succeeding. It appears the docs say I need a point and a size, but even that fails in the copy of VS 2003 I have. Exactly what are they trying...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
6
by: CSharper | last post by:
I am trying to use the following; I have an array with bunch of values in it. I am trying to find a value that contains part of the string I am passing eg string array = {"help","Csharp rocks"} ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.