我正嘗試在C#windows窗體應用程序中使用舊的C DLL。看起來像我使用錯誤的數據類型的輸出參數。在C#中使用DLL#
DLL源代碼:
#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif
EXPORT INT CALLBACK Complemento (LPSTR) ;
EXPORT INT CALLBACK MatchCode (LPSTR, LPSTR, LPSTR, LPSTR);
C#DLL導出
[DllImport(
@"<MYDIR>Fonetica.dll",
CharSet = CharSet.Ansi)]
public static extern int MatchCode(
String n,
String s,
String e,
[MarshalAs(UnmanagedType.LPStr)] out String retorno);
C#DLL使用碼
String match = String.Empty;
MatchCode(pefi.Nome, String.Empty, String.Empty, out match);
樹第一參數被輸入,最後一個是輸出。
I'm收到此異常:
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=mscorlib
StackTrace:
at System.String..ctor(SByte* value)
at System.StubHelpers.CSTRMarshaler.ConvertToManaged(IntPtr cstr)
at WFMatchCode.Form1.MatchCode(String n, String s, String e, String& retorno)
at WFMatchCode.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\computecnica.alexand\Documents\Visual Studio 2010\Projects\WFMatchCode\WFMatchCode\Form1.cs:line 91
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WFMatchCode.Program.Main() in c:\users\computecnica.alexand\documents\visual studio 2010\Projects\WFMatchCode\WFMatchCode\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
你的C DLL似乎很奇怪。人們如何知道什麼是輸入和輸出是什麼?前三個參數至少應該是LPCSTR。輸出參數缺少一個參數,該數組可以寫多長時間。你會期望編組傳遞給這個函數作爲輸出參數嗎? char數組有多大? – nvoigt
@alexandre傳遞'out string'是通過使用'StringBuilder'完成的。搜索pinvoke'StringBuilder',你會發現很多例子。 – xanatos
@nvoigt非常感謝,但是這個源代碼是13歲,我不想改變它...謝謝無論如何。 –