小编Yoh*_*anu的帖子

C# DllIimport 非托管 C 代码应用程序 (exe) 文件抛出 AccessViolationException

我目前正在导出 Win32 应用程序的一些函数,以便从托管代码中调用它,但我陷入了 AccessViolationException。它是一个非常简单的 DllImport,具有简单的类型,但是一旦我在非托管应用程序中调用诸如 malloc 或 printf 之类的函数,它就会抛出异常。

这是代码示例: //C# 程序

static void Main(string[] args)
{
  uint result = MyClass.ExecuteCommand((byte)10);
  Console.WriteLine(result);
  Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

//C#类库

public const string AppicationExe= "Application.exe";

[DllImport(AppicationExe, EntryPoint = "ExecuteCommand")]
public static extern UInt32 ExecuteCommand(byte mybyte);
Run Code Online (Sandbox Code Playgroud)

// C 应用程序

__declspec(dllexport) UINT32 __stdcall ExecuteCommand(unsigned char mybyte)
{ 
  printf("Why is it so difficult to make it works !!!!!!");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c# unmanaged marshalling dllimport

5
推荐指数
1
解决办法
1718
查看次数

标签 统计

c# ×1

dllimport ×1

marshalling ×1

unmanaged ×1