简体   繁体   English

在C#中使用DLL

[英]Using DLL in C#

I´m trying to use a old C DLL in a C# windows forms app. 我试图在C#Windows窗体应用程序中使用旧的C DLL。 Seems like i´m using the wrong datatype for the output parameter. 好像我在为输出参数使用错误的数据类型。

DLL source code: 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 Export 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 usage code C#DLL使用代码

String match = String.Empty;

MatchCode(pefi.Nome, String.Empty, String.Empty, out match);

The tree first parameters are input and the last one is an output. 树的第一个参数是输入,最后一个是输出。

I´m receiving this exception: 我收到此异常:

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: 
[DllImport( @"Fonetica.dll", CharSet = CharSet.Ansi)]
public static extern int MatchCode(String n, String s, String e, StringBuilder retorno);

You then must pass a StringBuilder that has enough capacity to take whatever gets written to it. 然后,您必须传递一个StringBuilder,它具有足够的容量来处理写入其中的所有内容。 The same way you must pass an array that's large enough in C: 您必须以相同的方式传递在C中足够大的数组:

var capacity = 1000; // change this to whatever you need
var buffer = new StringBuilder(capacity);

var result = MatchCode("test", "test", "test", buffer);

var output = buffer.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM