简体   繁体   English

如何使用C#创建非托管DLL?

[英]How to create unmanaged DLL using C#?

I want to create a DLL using C#, which can be then called from VBA in the following manner - 我想使用C#创建一个DLL,然后可以通过以下方式从VBA调用它 -

Private Declare Function invokePath Lib "\\shared_computer\Projects\somedll\myDLL.dll" (ByVal strUName As String, ByVal strSFile As String) As String

The idea is, if the path of the DLL is changed I need to only update the path in the Private Declare function line.... 这个想法是,如果DLL的路径改变了,我只需要更新私有声明功能行中的路径....

I have searched a lot for it, and also not sure whether this arrangement is possible - where we can call a DLL function from a network path without referencing or registering. 我已经搜索了很多,也不确定这种安排是否可行 - 我们可以在没有引用或注册的情况下从网络路径调用DLL函数。

Actually you can. 其实你可以。 Check out unmanagedexports It is a complete walk-through and you'll be able to create entry points like this: 查看unmanagedexports这是一个完整的演练,你将能够创建这样的入口点:

[DllExport("GenerateSomehting", CallingConvention = CallingConvention.Cdecl)]
public static int GenerateSomehting(int somevalue, ref IntPtr pointer)

and call it just like any other unmanaged dll. 并像任何其他非托管DLL一样调用它。

[DllImport("MyGenerator.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int GenerateSomehting(int somevalue, ref IntPtr pointer);

You can not create unmanaged DLLs with C#, as C# is a .NET only language. 您无法使用C#创建非托管DLL,因为C#是仅支持.NET的语言。 Use C++. 使用C ++。

The only way to use a .NET DLL from unmanaged code (for example VBA) is to use COM interop. 从非托管代码(例如VBA)使用.NET DLL的唯一方法是使用COM互操作。

Check P/Invoke here, The article contains a useful table of how to translate managed to unmanaged types, short code examples (in C#), tips and a list of resources. 在这里检查P / Invoke,本文包含一个有用的表,说明如何将托管类型转换为非托管类型,短代码示例(在C#中),提示和资源列表。

Essential P/Invoke 必要的P / Invoke

C# Dll cannot export functions that can be called by any other non-CLR code. C#Dll无法导出任何其他非CLR代码可以调用的函数。

This is not possible. 这是不可能的。

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

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