[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

how to pass a struct with a point to a mfc dll

zhanglr

5/23/2007 2:59:00 PM

Hi,

I am a new guy in C#.

I try to pass a struct with a point to a mfc dll. But I am failed. Anybody
knows what I must do?

The following is my code:
------------------------------------
C++ dll:
class MyStrStruct
{
public:
int size;
int* buffer;
};

extern "C" bool PASCAL EXPORT fMFCDLL(MyStrStruct* pObj)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// normal function body here

for(int i = 0; i < pObj->size;++i)
{
pObj->buffer[i] += 1;
}

return true;
}

-----------------------------
c#:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MyStrStruct
{
public int size;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType =
UnmanagedType.I4)]
public int[] buffer;
}

[DllImport("MFCDll.dll", CharSet = CharSet.Auto)]
private static extern bool fMFCDLL([In,Out] MyStrStruct obj);

private void button1_Click(object sender, EventArgs e)
{
try
{
MyStrStruct obj = new MyStrStruct();

obj.size = 10;
obj.buffer = new int[obj.size];

for (int i = 0; i < obj.size; ++i)
{
obj.buffer[i] = i;
}

fMFCDLL(obj);

string strOutput="";
for (int i = 0; i < obj.size; ++i)
{
strOutput += obj.buffer[i].ToString() + " ";
}
MessageBox.Show(strOutput);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

Best Regards,
Michael
1 Answer

zhanglr

5/23/2007 3:09:00 PM

0

Hi,

I have found the solution. It is:

public IntPtr buffer;
....
obj.buffer = Marshal.UnsafeAddrOfPinnedArrayElement(ari, 0);

Anyway, thanks.

Best Regards,
Michael

"zhanglr" wrote:

> Hi,
>
> I am a new guy in C#.
>
> I try to pass a struct with a point to a mfc dll. But I am failed. Anybody
> knows what I must do?
>
> The following is my code:
> ------------------------------------
> C++ dll:
> class MyStrStruct
> {
> public:
> int size;
> int* buffer;
> };
>
> extern "C" bool PASCAL EXPORT fMFCDLL(MyStrStruct* pObj)
> {
> AFX_MANAGE_STATE(AfxGetStaticModuleState());
> // normal function body here
>
> for(int i = 0; i < pObj->size;++i)
> {
> pObj->buffer[i] += 1;
> }
>
> return true;
> }
>
> -----------------------------
> c#:
> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
> public class MyStrStruct
> {
> public int size;
> [MarshalAs(UnmanagedType.ByValArray, ArraySubType =
> UnmanagedType.I4)]
> public int[] buffer;
> }
>
> [DllImport("MFCDll.dll", CharSet = CharSet.Auto)]
> private static extern bool fMFCDLL([In,Out] MyStrStruct obj);
>
> private void button1_Click(object sender, EventArgs e)
> {
> try
> {
> MyStrStruct obj = new MyStrStruct();
>
> obj.size = 10;
> obj.buffer = new int[obj.size];
>
> for (int i = 0; i < obj.size; ++i)
> {
> obj.buffer[i] = i;
> }
>
> fMFCDLL(obj);
>
> string strOutput="";
> for (int i = 0; i < obj.size; ++i)
> {
> strOutput += obj.buffer[i].ToString() + " ";
> }
> MessageBox.Show(strOutput);
> }
> catch(Exception ex)
> {
> MessageBox.Show(ex.Message);
> }
> }
> }
>
> Best Regards,
> Michael