Using a dll inside of C#使用一个dll在C#中。

huangapple go评论99阅读模式
英文:

Using a dll inside of C#

问题

I made a struct inside a dll called "TestData" then after compiling that dll in the .lib format I tested it with a kernel driver I made in C. This worked fine but I want to be able to communicate and pass this struct from my C driver to my C# desktop application. This way I only have to modify the struct in one place and have the changes take effect in both the driver and my C# application. All that being said I am rather new to C# and I am having a hard time getting the dll or lib (I tried it in both versions) to work correctly. I first tried adding a reference but received a "Please make sure that file is accessible, and that it is a valid assembly or COM component" error. Next, I stumbled across this which is similar with the difference being the op was asking to call a function while I just wanted to access a struct. But either way I am confused by the top-rated answer because it appears not to reference anything specific from the dll.

I know structs can be made inside C# and found an example of this being done in the following way...

        [StructLayout(LayoutKind.Sequential)]
        public struct EXAMPLE
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public char[] ex1;
            public ushort ex2;
        }

It looks manageable just with the addition of a few extra steps, but it would be ideal if I could just use the dll (or more likely the lib so it can be statically loaded).

英文:

I made a struct inside a dll called TestData then after compiling that dll in the .lib format I tested it with a kernel driver I made in C. This worked fine but I want to be able to communicate and pass this struct from my C driver to my C# desktop application. This way I only have to modify the struct in one place and have the changes take effect in both the driver and my C# application. All that being said I am rather new to C# and I am having a hard time getting the dll or lib (I tried it in both versions) to work correctly. I first tried adding a reference but received a Please make sure that file is accessible, and that it is valid assembly or COM component error. Next, I stumbled across this which is similar with the difference being the op was asking to call a function while I just wanted to access a struct. But either way I am confused by the top-rated answer because it appears not to reference anything specific from the dll.

I know structs can be made inside C# and found an example of this being done in the following way...

        [StructLayout(LayoutKind.Sequential)]
        public struct EXAMPLE
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public char[] ex1;
            public ushort ex2;
        }

It looks manageable just with the addition of a few extra steps, but it would be ideal if I could just use the dll (or more likely the lib so it can be statically loaded).

答案1

得分: 0

".NET显然有自己的元数据格式,描述了类型。原生前身是一个“类型库”,主要由OLE/COM/ActiveX使用(.NET的原始名称是“ComPlus”),不是从您编写代码的源语言生成的,而是从专用的IDL“接口描述语言”生成的。它可以存储在单独的文件中或嵌入在DLL中作为资源(而不是导出),并可用于自动生成多种语言的源代码。

但是,虽然IDL和类型库确实描述了类型上可用的操作,但它们通常避免了数据的内部内存布局,仅包括函数表(在C++中为v-table)。而数据的内存布局似乎是您关心的问题。您可以转向类似的技术,以可移植的方式描述数据结构,例如ASN.1或protobuf,然后从该与语言无关的定义中生成C和C#源代码。"

英文:

DLL "exports" can only be addresses of functions and data, and the caller has to know the correct data type including layout.

.NET obviously has its own metadata format which describes types. The native predecessor is a "type library" which is mostly used by OLE/COM/ActiveX (.NET's original name being "ComPlus"), is not generated from the source language you write your code in, but from a dedicated IDL "interface description language". It is stored in either a separate file or embedded in the DLL as a resource (not an export), and can be used to automatically generate source code in a wide variety of languages.

But while IDL and type libraries do describe the operations available on a type, they typically avoid the internal memory layout of data, only the table of functions (v-table in C++). And the memory layout of data seems to be what you're after. You might turn to similar technologies that do describe data structures in a portable way, such as ASN.1 or protobuf, and from that language-independent definition, generate both C and C# source code.

huangapple
  • 本文由 发表于 2023年8月5日 05:53:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定