HighTechTalks DotNet Forums  

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

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss how to pass a struct with a point to a mfc dll in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
zhanglr
 
Posts: n/a

Default how to pass a struct with a point to a mfc dll - 05-23-2007 , 10:59 AM






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

Reply With Quote
  #2  
Old   
zhanglr
 
Posts: n/a

Default RE: how to pass a struct with a point to a mfc dll - 05-23-2007 , 11:09 AM






Hi,

I have found the solution. It is:

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

Anyway, thanks.

Best Regards,
Michael

"zhanglr" wrote:

Quote:
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

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.