Click here to Skip to main content
15,912,400 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
shusong16-Apr-05 15:00
shusong16-Apr-05 15:00 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
Heath Stewart16-Apr-05 22:53
protectorHeath Stewart16-Apr-05 22:53 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
shusong16-Apr-05 23:19
shusong16-Apr-05 23:19 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
Heath Stewart16-Apr-05 23:47
protectorHeath Stewart16-Apr-05 23:47 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
shusong17-Apr-05 1:04
shusong17-Apr-05 1:04 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
shusong17-Apr-05 15:15
shusong17-Apr-05 15:15 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
Heath Stewart17-Apr-05 16:28
protectorHeath Stewart17-Apr-05 16:28 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
shusong17-Apr-05 16:49
shusong17-Apr-05 16:49 
1, I build a Regular Dll using shared MFC dll as follows:

extern "C" void PASCAL EXPORT GetValues(CStringArray & stringArray)<br />
{<br />
AFX_MANAGE_STATE(AfxGetStaticModuleState());<br />
CString a = "a"; <br />
CString b = "b";<br />
<br />
stringArray.Add(a);<br />
stringArray.Add(b);<br />
}

2,build a c# console project named text.exe as follows:
using System;<br />
using System.Runtime.InteropServices;<br />
<br />
namespace CSharpProject<br />
{<br />
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]<br />
    class CStringArray{<br />
        public string[] m_pData;<br />
        IntPtr m_nSize; <br />
        IntPtr m_nMaxSize; <br />
        IntPtr m_nGrowBy;<br />
    }<br />
<br />
    class Class1<br />
    { <br />
        [DllImport("MFCDLL.dll", CharSet=CharSet.Auto)]<br />
        public static extern void GetValues(out CStringArray strarr);<br />
<br />
        [STAThread]<br />
        static void Main(string[] args)<br />
        {<br />
            CStringArray stringArray = new CStringArray();<br />
            GetValues(out stringArray); // thow exception<br />
        }<br />
    }<br />
}

3, Copy MFCDLL.dll to the directory of text.exe.
4,double press text.exe ,but program throws exception:System.ExecutionEngineException. When debugging the text.exe,variable:stringArray is undefined value.I think maybe there is some problem with the structure of class CStringArray.

P.S.
1. If debugging the program yourself ,you should diagnose the problem.
2. In ..\MFC\include\AFXCOLL.h, In CStringArray,there is:
// Implementation<br />
protected:<br />
	CString* m_pData;   // the actual array of data<br />
	int m_nSize;     // # of elements (upperBound - 1)<br />
	int m_nMaxSize;  // max allocated<br />
	int m_nGrowBy;   // grow amount


so ,i modify your CStringArray:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]<br />
class CStringArray{ <br />
 public string[] m_pData;<br />
 int m_nSize;  <br />
 int m_nMaxSize;<br />
 int m_nGrowBy;<br />
}
But it does not work,too.

Thank you very much.
GeneralRe: How to use CStringArray in C# using p/invoke Pin
Heath Stewart18-Apr-05 6:59
protectorHeath Stewart18-Apr-05 6:59 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
ttcchh200812-Jun-10 3:14
ttcchh200812-Jun-10 3:14 
Questionhow to implement WHITEBOARD for many users Pin
xinxin_52515-Apr-05 23:33
xinxin_52515-Apr-05 23:33 
AnswerRe: how to implement WHITEBOARD for many users Pin
Colin Angus Mackay15-Apr-05 23:48
Colin Angus Mackay15-Apr-05 23:48 
GeneralURI format not supported Pin
Adnan Siddiqi15-Apr-05 22:29
Adnan Siddiqi15-Apr-05 22:29 
GeneralRe: URI format not supported Pin
Heath Stewart16-Apr-05 8:18
protectorHeath Stewart16-Apr-05 8:18 
GeneralRe: URI format not supported Pin
Adnan Siddiqi17-Apr-05 6:00
Adnan Siddiqi17-Apr-05 6:00 
GeneralRe: URI format not supported Pin
Heath Stewart17-Apr-05 16:24
protectorHeath Stewart17-Apr-05 16:24 
GeneralFun with collections... Pin
tantiboh15-Apr-05 22:13
tantiboh15-Apr-05 22:13 
GeneralRe: Fun with collections... Pin
Colin Angus Mackay15-Apr-05 23:37
Colin Angus Mackay15-Apr-05 23:37 
GeneralRe: Fun with collections... Pin
tantiboh16-Apr-05 6:29
tantiboh16-Apr-05 6:29 
GeneralVS.NET 2003 IDE Pin
pmasknguyen15-Apr-05 18:43
pmasknguyen15-Apr-05 18:43 
GeneralRe: VS.NET 2003 IDE Pin
leppie15-Apr-05 21:05
leppie15-Apr-05 21:05 
GeneralWeb Page Generator using C... Pin
ZMAN072815-Apr-05 14:16
ZMAN072815-Apr-05 14:16 
GeneralRe: Web Page Generator using C... Pin
Yulianto.15-Apr-05 16:07
Yulianto.15-Apr-05 16:07 
Questionget size of picture folder?? Pin
Anonymous15-Apr-05 12:58
Anonymous15-Apr-05 12:58 
AnswerRe: get size of picture folder?? Pin
Nicholas Cardi15-Apr-05 13:21
Nicholas Cardi15-Apr-05 13:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.