[問題] C#中DllImport 函數呼叫
向大家請教一些問題,
其實我還是不太了解要如何才能讓同一個function在多個.cs中被呼叫,
是只要using進來可以嗎?還是namespace要相同才可以?
情況是這樣的,手上拿到對方給的dll檔,
在同一個專案中另開了一個imitatePacket.cs和Form1.cs,
在imitatePacket.cs中將此dll import進來,
imitatePacket.cs程式碼如下:
using System.Runtime.InteropServices;
namespace imitatePacket
{
public class imPacket
{
[DllImport("Bus.dll", EntryPoint = "bus_GetNumDevices",
CallingConvention = CallingConvention.StdCall)]
public static extern BUS_STATUS Bus_GetDevices(ref uint numofDevices,
ushort vid, ushort pid);
public static BUS_STATUS Result = BUS_STATUS.BUS_DEVICE_ACCESS_ERROR;
public static bool API_GetDevices(ushort vid, ushort pid)
{
uint numDevices = 0;
Result = bus_GetDevices(ref numDevices, vid, pid);
if (Result == BUS_STATUS.BUS_SUCCESS)
{
return true;
}
else
{
return false;
}
}
}
}
=================================================
接下來要在Form1.cs中呼叫在imitatePeckte.cs中dll import進來的function,
這時候先加入using imitatePacket;(??)
但在Form1.cs中呼叫imitatePacket.cs中的class imPacket中的成員API_GetDevices依然
是出現錯誤的訊息。
Form1.cs的程式碼如下
using imitatePacket;
namespace formosa
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ushort uvid=4292, upid=60048;
imPacket.API_GetDevices(uvid, upid);//未能成功呼叫
}
}
======================================
請問大家我是在哪個地方做錯或是沒有注意到呢?
是要把imitatePacket.cs中的namespace改成跟Form1.cs一樣的formosa呢?
還是只要在Form1.cs中加入using imitatePacket;才對呢?
試過上面兩種方法還是產生錯誤。
或是有其它的地方做錯,也請大家指教一下,謝謝。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.72.229.163
推
07/22 18:10, , 1F
07/22 18:10, 1F
→
07/22 18:10, , 2F
07/22 18:10, 2F
→
07/22 18:11, , 3F
07/22 18:11, 3F
→
07/22 18:11, , 4F
07/22 18:11, 4F
→
07/22 18:12, , 5F
07/22 18:12, 5F
→
07/22 18:13, , 6F
07/22 18:13, 6F
→
07/22 18:14, , 7F
07/22 18:14, 7F
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章