我在 VS2012 中用 C# 语言调用 DLL(C 编写)。mylib.dll 是我要在 C# 中调用的本机 dll,而 mylib.dll 也将调用另一个 mylib_another.dll。
C函数声明为:
extern DECLSPEC_DLL BOOLEAN_TYPE SetConnection(char *dev, char *addr);
在我的 C# 文件中,我将其声明为:
[DllImport("C:\\mylib.dll", EntryPoint = "SetConnection", CharSet = CharSet.Auto)]
public static unsafe extern int SetConnection(StringBuilder dev, StringBuilder addr);
当我在代码中调用它时,我发现字符串只传递了一个字符,当我将 dev 作为“USB”传递时,本机 DLL 实际上只得到一个“U”。
如果我将声明更改为:
[DllImport("C:\\mylib.dll", EntryPoint = "SetConnection", CharSet = CharSet.Ansi)]
public static unsafe extern int SetConnection(StringBuilder dev, StringBuilder addr);
然后它将引发 System.AccessViolationException Exception :
System.Reflection.TargetInvocationException was unhandled
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at QC.QTMDotNetKernel.DotNetKernel.RunDotNetTest(Object stateObject)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
InnerException: System.AccessViolationException
HResult=-2147467261
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
有什么想法吗?
已解决如下(第一个问题的答案,第二个问题的答案,那是因为mylib.dll调用了另一个本机dll,系统没有找到它)。
这里有一个类似的问题: