【发布时间】:2017-03-01 21:42:45
【问题描述】:
在我的 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;
}
【问题讨论】:
-
您的标题和您选择的标签之一表示
C#,但代码和消息看起来像C++-- 它是什么? -
这是调用 DLL 的 C# 代码(我认为它是用 C++ 编写的)我可以做些什么来消除我帖子中的困惑吗?
-
C# 控制台应用程序通常需要一个名为
Main()的入口点,请参阅下面的 MSDN article 讨论System.EntryPointNotFoundException——尤其是 Remarks 下的前几点.解决此问题将至少解决您遇到的问题之一。 -
嗯,我实际上是在使用 Visual Studio 制作一个 Windows 窗体应用程序,所以
Main()方法在Program.cs中。我认为我的问题是它没有在我正在使用的 DLL 中找到函数AT91Boot_Scan...尽管如此,Remarks 下的其他点提出了一些有趣的点,例如修饰名称...我会尝试一下。谢谢! -
我更仔细地查看了
sam-ba.dll的一些文档,似乎这个DLL 是一个COM / OLE 对象。我能够添加对 TLB 文件SAMBA_DLL.tlb的引用,添加一个using SAMBA_DLLLib;,然后使用SAMBA_DLLLib.ISAMBADLL mySamba = new SAMBA_DLLLib.SAMBADLL();实例化一个对象然后当我在设计时使用该对象变量mySamba.时,Intellisense 知道' 方法和签名是什么样的,包括您感兴趣的AT91Boot_Scan()方法。我认为这是您希望使用这个特定的 DLL 和 C# 的方式。
标签: c# atmel entrypointnotfoundexcept