使用 *.dll 作为 "Pinvoke collection"
Use *.dll as "Pinvoke collection"
我有一个包含多个 类 的项目,对我来说应该像 P/Invoke 集合一样工作。
例如
namespace Win32
{
static class Winspool
{
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint GetPrinterData(
IntPtr hPrinter,
string pValueName,
out uint pType,
byte[] pData,
uint nSize,
out uint pcbNeeded);
}
}
这个项目要大得多,只有 DllImports、[StructLayout(LayoutKind.Sequential)]、Flags、Structs、Enums 等。很多东西来自 win32 api.
该项目应该编译成一个 dll 文件,因为我需要在我的项目中到处调用几个 Win32 函数并且不希望我的代码中出现 dllimport declerations。
我现在的问题是:是否可以在任何其他 C# 项目中使用此 dll 并调用导入的函数?
我尝试通过引用添加我的 dll,但无法从我的 dll 中调用任何内容。
因为您的 class 不是 public
,它将具有 internal
的默认可见性,并且从其自身的程序集外部不可见。
所以如果你做到了 public
:
public static class Winspool
{
}
然后您可以从其他程序集访问它:
Win32.Winspool.GetPrinterData(...);
我有一个包含多个 类 的项目,对我来说应该像 P/Invoke 集合一样工作。
例如
namespace Win32
{
static class Winspool
{
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint GetPrinterData(
IntPtr hPrinter,
string pValueName,
out uint pType,
byte[] pData,
uint nSize,
out uint pcbNeeded);
}
}
这个项目要大得多,只有 DllImports、[StructLayout(LayoutKind.Sequential)]、Flags、Structs、Enums 等。很多东西来自 win32 api.
该项目应该编译成一个 dll 文件,因为我需要在我的项目中到处调用几个 Win32 函数并且不希望我的代码中出现 dllimport declerations。
我现在的问题是:是否可以在任何其他 C# 项目中使用此 dll 并调用导入的函数?
我尝试通过引用添加我的 dll,但无法从我的 dll 中调用任何内容。
因为您的 class 不是 public
,它将具有 internal
的默认可见性,并且从其自身的程序集外部不可见。
所以如果你做到了 public
:
public static class Winspool
{
}
然后您可以从其他程序集访问它:
Win32.Winspool.GetPrinterData(...);