简体   繁体   English

使用* .dll作为“ Pinvoke集合”

[英]Use *.dll as “Pinvoke collection”

I got a Project with several classes which should work like a P/Invoke collection for me. 我有一个包含几个类的项目,对我来说,它们应该像P / Invoke集合一样工作。

For example 例如

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);
    }
}

The project is much bigger and got only DllImports, [StructLayout(LayoutKind.Sequential)], Flags, Structs, Enums etc. a lot of things from the win32 api. 该项目更大,并且从Win32 API中仅获得了DllImports,[StructLayout(LayoutKind.Sequential)],Flags,Structs,Enums等很多东西。

The project should be compiled to a dll-file because I need to call several Win32 functions here and there in my projects and dont want the dllimport declerations in my code. 该项目应编译为一个dll文件,因为我需要在我的项目中四处调用几个Win32函数,并且不希望我的代码中出现dllimport脱节。

My question now: Is it possible to use this dll in any other C#-Project and call the imported functions? 现在我的问题是:是否可以在其他任何C#项目中使用此dll并调用导入的函数?

I've tried to add my dll via reference but I was not able to call anything out of my dll. 我试图通过引用添加我的dll,但无法从dll中调用任何内容。

Because your class is not public , it will have the default visibility of internal and will not be visible from outside its own assembly. 因为您的类不是public类,所以它将具有internal的默认可见性,并且在其自己的程序集外部将不可见。

So if you make it public : 因此,如果您将其public

public static class Winspool
{
}

Then you can access it from other assemblies: 然后,您可以从其他程序集中访问它:

Win32.Winspool.GetPrinterData(...);

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

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