简体   繁体   English

如何在C#中使用DLL函数而不添加DLL作为参考?

[英]How do I use a DLL function in C# without adding the DLL as reference?

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). 我正在尝试使用无法通过Visual Studio作为参考添加的DLL中的函数(出现一条消息,提示“无法添加参考”)。 However, I've gotten instructions from the creator of the DLL and they suggested I use them like this in VB.Net: 但是,我从DLL的创建者那里得到了说明,他们建议我在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#. 那行得通,但是现在我想用C#编写程序。 How do I "translate" that declaration to C#? 如何将该声明“翻译”为C#?

Additional: In C++ the declaration comes in a *.h file with these lines: 附加:在C ++中,声明位于带有以下行的* .h文件中:

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

You should create the method(s) you want to use in C#, make them extern and add a [DllImport] attribute. 您应该创建要在C#中使用的方法,使其成为extern并添加[DllImport]属性。 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 参见https://msdn.microsoft.com/zh-cn/library/aa984739%28v=vs.71%29.aspx

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

public static extern unsafe Int32 Prn_Init(void); 公共静态外部不安全Int32 Prn_Init(void);

("unsafe" is optional) (“不安全”是可选的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM