C# EntryPointNotFoundException 异常
C# EntryPointNotFoundException
在我的 C# 程序中,我正在调用 sam-ba.dll
中的 AT91Boot_Scan
函数。在这个 DLL 的 documentation 中,这个函数的签名是 void AT91Boot_Scan(char *pDevList)
不幸的是,无论我尝试什么,我都会收到 EntryPointNotFoundException
和 ArgumentNullException
错误:
Exception thrown at 0x75E3C54F in MyApp.exe: Microsoft C++ exception: EEMessageException at memory location 0x0038E304.
Exception thrown: 'System.EntryPointNotFoundException' in MyApp.exe
Unable to find an entry point named 'AT91Boot_Scan' in DLL 'sam-ba.dll'.
Exception thrown: 'System.ArgumentNullException' in System.Windows.Forms.dll
我的代码如下,我哪里做错了?
[DllImport("sam-ba.dll")]
unsafe public static extern void AT91Boot_Scan(char* pDevList);
unsafe private string[] LoadDeviceList()
{
const int MAX_NUM_DEVICES = 10;
const int BYTES_PER_DEVICE_NAME = 100;
string[] deviceNames = new string[MAX_NUM_DEVICES];
try
{
unsafe
{
// A pointer to an array of pointers of size MAX_NUM_DEVICES
byte** deviceList = stackalloc byte*[MAX_NUM_DEVICES];
for (int n = 0; n < MAX_NUM_DEVICES; n++)
{
// A pointer to a buffer of size 100
byte* deviceNameBuffer = stackalloc byte[100];
deviceList[n] = deviceNameBuffer;
}
// Is casting byte** to char* even legal?
AT91Boot_Scan((char*)deviceList);
// Read back out the names by converting the bytes to strings
for (int n = 0; n < MAX_NUM_DEVICES; n++)
{
byte[] nameAsBytes = new byte[BYTES_PER_DEVICE_NAME];
Marshal.Copy((IntPtr)deviceList[n], nameAsBytes, 0, BYTES_PER_DEVICE_NAME);
string nameAsString = System.Text.Encoding.UTF8.GetString(nameAsBytes);
deviceNames[n] = nameAsString;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return deviceNames;
}
按照 David Tansey 在评论中的解决方案,我应该这样做的方式是添加 sam-ba.dll
作为 COM 引用(解决方案资源管理器 > 引用 > 添加引用 > 浏览)。然后只需实例化一个 ISAMBADLL
class 对象并通过该 class 调用方法。
在我的 C# 程序中,我正在调用 sam-ba.dll
中的 AT91Boot_Scan
函数。在这个 DLL 的 documentation 中,这个函数的签名是 void AT91Boot_Scan(char *pDevList)
不幸的是,无论我尝试什么,我都会收到 EntryPointNotFoundException
和 ArgumentNullException
错误:
Exception thrown at 0x75E3C54F in MyApp.exe: Microsoft C++ exception: EEMessageException at memory location 0x0038E304.
Exception thrown: 'System.EntryPointNotFoundException' in MyApp.exe
Unable to find an entry point named 'AT91Boot_Scan' in DLL 'sam-ba.dll'.
Exception thrown: 'System.ArgumentNullException' in System.Windows.Forms.dll
我的代码如下,我哪里做错了?
[DllImport("sam-ba.dll")]
unsafe public static extern void AT91Boot_Scan(char* pDevList);
unsafe private string[] LoadDeviceList()
{
const int MAX_NUM_DEVICES = 10;
const int BYTES_PER_DEVICE_NAME = 100;
string[] deviceNames = new string[MAX_NUM_DEVICES];
try
{
unsafe
{
// A pointer to an array of pointers of size MAX_NUM_DEVICES
byte** deviceList = stackalloc byte*[MAX_NUM_DEVICES];
for (int n = 0; n < MAX_NUM_DEVICES; n++)
{
// A pointer to a buffer of size 100
byte* deviceNameBuffer = stackalloc byte[100];
deviceList[n] = deviceNameBuffer;
}
// Is casting byte** to char* even legal?
AT91Boot_Scan((char*)deviceList);
// Read back out the names by converting the bytes to strings
for (int n = 0; n < MAX_NUM_DEVICES; n++)
{
byte[] nameAsBytes = new byte[BYTES_PER_DEVICE_NAME];
Marshal.Copy((IntPtr)deviceList[n], nameAsBytes, 0, BYTES_PER_DEVICE_NAME);
string nameAsString = System.Text.Encoding.UTF8.GetString(nameAsBytes);
deviceNames[n] = nameAsString;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return deviceNames;
}
按照 David Tansey 在评论中的解决方案,我应该这样做的方式是添加 sam-ba.dll
作为 COM 引用(解决方案资源管理器 > 引用 > 添加引用 > 浏览)。然后只需实例化一个 ISAMBADLL
class 对象并通过该 class 调用方法。