找到你要的答案

Q:How do I use a DLL function in C# without adding the DLL as reference?

Q:我怎样使用一个DLL函数在C #不添加DLL作为参考?

I'm trying to use functions from a DLL that can't be added as Reference through Visual Studio (a message saying "reference cannot be added" appears). However, I've gotten instructions from the creator of the DLL and they suggested I use them like this in VB.Net:

Private Declare Function Prn_Init Lib "VAx_VPOS396_APPAPI.dll" () As Integer

That works, but now I want to write a program in C#. How do I "translate" that declaration to C#?

Additional: In C++ the declaration comes in a *.h file with these lines:

#ifndef  _VPOS396DLL_API_H
#define  _VPOS396DLL_API_H
VPOS396_DLL_API     int Prn_Init(void);

我想用一个DLL不能被添加通过Visual Studio引用函数(一个信息说:“参考不能添加”出现)。然而,我从DLL的创造者得到指示,他们建议我使用他们这样在VB。NET:

Private Declare Function Prn_Init Lib "VAx_VPOS396_APPAPI.dll" () As Integer

的作品,但现在我想在C #编写程序。我如何“翻译”,C #宣言?

另外:在C++的声明是在一个*。这些线h文件:

#ifndef  _VPOS396DLL_API_H
#define  _VPOS396DLL_API_H
VPOS396_DLL_API     int Prn_Init(void);
answer1: 回答1:

You should create the method(s) you want to use in C#, make them extern and add a [DllImport] attribute. For example:

[DllImport("kernel32.dll")]
static extern bool Beep(uint dwFreq, uint dwDuration);

See https://msdn.microsoft.com/en-us/library/aa984739%28v=vs.71%29.aspx

你应该创建方法(S)你想使用C #,使外部添加一个[上]属性。例如:

[DllImport("kernel32.dll")]
static extern bool Beep(uint dwFreq, uint dwDuration);

看到HTTPS:/ / MSDN。微软。COM /我们/图书馆/ aa984739 % 28v =:7100 % 29.aspx

answer2: 回答2:

[System.Runtime.InteropServices.DllImport("VAx_VPOS396_APPAPI.dll")]

public static extern unsafe Int32 Prn_Init(void);

("unsafe" is optional)

[系统。运行时,即可看到。DllImport(“vax_vpos396_appapi .dll”)]

公共静态外部不安全prn_init int32(无效);

(“不安全”是可选的)

c#  dll  namespaces  declaration