简体   繁体   English

在 C# COM-DLL 项目中使用外部 DLL 进行 MS-Access 使用

[英]Using external DLL's in C# COM-DLL project for MS-Access usage

The user's main application is ms-access (with ms-sql server).用户的主要应用是ms-access(使用ms-sql server)。 Ton of code in vba. vba 中的大量代码。 One of the main features i have to manifest is controlling my client's voip telephony.我必须展示的主要功能之一是控制我客户的 voip 电话。 My plan is to make a COM DLL that the ms-access can use.我的计划是制作一个 ms-access 可以使用的 COM DLL。

As of now, i've managed to write a C# project (vs2019 .net framework) that handles the voip pbx well, using json, and do all missions necessary, such as invoking calls, sending sms, get calls list, recodings list, get recordings wav files and convert them to mp3.到目前为止,我已经设法编写了一个 C# 项目(vs2019 .net 框架),它可以很好地处理 voip pbx,使用 json,并完成所有必要的任务,例如调用呼叫、发送短信、获取呼叫列表、重新编码列表,获取录音 wav 文件并将它们转换为 mp3。 I use Newtopsoft.Json and NAudio.Lame.我使用 Newtopsoft.Json 和 NAudio.Lame。

BUT it only works as a standalone .net project or exe.但它只能作为独立的 .net 项目或 exe 工作。 As a COM DLL (class library) - ms-access can handle and use it UNTIL it gets to the part where the code uses ANY external dll's functionality - and it crashes (with newtonsoft) or just not working (with naudio), although the dll's are in the working folder.作为 COM DLL(类库) - ms-access 可以处理和使用它,直到它到达代码使用任何外部 dll 功能的部分 - 它崩溃(使用 newtonsoft)或只是不工作(使用 naudio),尽管dll 位于工作文件夹中。

I know i have to found a way to make these external dll's to work under the office (32bit) enviroment.我知道我必须找到一种方法让这些外部 dll 在办公室(32 位)环境下工作。 I was trying and guessing many code samples for a couple of days.几天来,我一直在尝试和猜测许多代码示例。 I tried to follow the Assembly.Load/From/File examples to dynamically load and include the dll's, but i failed to assimilate it in my code or even to understand it enough.我试图按照 Assembly.Load/From/File 示例动态加载和包含 dll,但我未能在我的代码中吸收它,甚至无法充分理解它。

I hope i'll get a solution here.我希望我能在这里得到解决方案。 Thanks :)谢谢 :)

Hum, I am able to create a COM object for use with Access.嗯,我能够创建一个用于 Access 的 COM 对象。 External library code (in my case Newton soft) for serializing data works just fine when that class code is used as a COM object from Access.当该类代码用作 Access 的 COM 对象时,用于序列化数据的外部库代码(在我的例子中是 Newton soft)工作得很好。 You don't mention if the other libraries are managed code, or external win32 .dll?您没有提到其他库是托管代码还是外部win32 .dll? I have however used un-managed .dll's with a .net COM object, and then have consumed that from MS-access.然而,我已经将非托管的 .dll 与 .net COM 对象一起使用,然后从 MS-access 中使用了它。 The only issue would be in project explorer is to ensure that you have a copy local setting = true for the external referenced .net .dll libraries.唯一的问题是在项目资源管理器中,确保您有一个副本 local setting = true 用于外部引用的 .net .dll 库。 (And that is the default, so I am sure you do. The next issue of course is of course is there any use of configuration files? (app.config) etc.? The reason for this of course that since your application is being launched with msaccess.exe, then the config and settings will be assumed to be in the office install folder, and not the local application. As a result, it not all so practical to move/copy those app.config files to the office folder where msaccess.exe resides. And the config file name will not only have to be in the same folder, but also named. (这是默认设置,所以我相信你会这样做。当然,下一个问题当然是有没有使用配置文件?(app.config)等?这样做的原因当然是因为您的应用程序正在使用 msaccess.exe 启动,那么配置和设置将被假定在 office 安装文件夹中,而不是本地应用程序。因此,将这些 app.config 文件移动/复制到 office 文件夹并不是那么实用msaccess.exe 所在的位置。配置文件名不仅要在同一个文件夹中,还要命名。

Msaccess.exe.config. Msaccess.exe.config。

In place of代替

YourDotNet.exe.config YourDotNet.exe.config

Now of course, if you build your project as a .net exec?现在当然,如果您将项目构建为 .net exec? Well then of course any config settings (application ones, not user) will be placed in this config file, and of course that file will have to be placed in the same folder as the .exe file.那么当然任何配置设置(应用程序设置,而不是用户)都将放置在此配置文件中,当然该文件必须与 .exe 文件放置在同一文件夹中。

So, one might add a “test” function (any function is a method of that class) to the class that you test/call from VBA to ensure that such settings are being seen and read (if you are using any settings).因此,可以向您从 VBA 测试/调用的类添加一个“测试”函数(任何函数都是该类的方法),以确保可以看到和读取此类设置(如果您正在使用任何设置)。

Eg:例如:

System.Reflection.Assembly.GetExecutingAssembly().Location + ".config" System.Reflection.Assembly.GetExecutingAssembly().Location + ".config"

So, return that to access and display this result.因此,返回它以访问并显示此结果。

The other issue is of course to FORCE your project to x86.另一个问题当然是强制您的项目到 x86。 I would not use “any” CPU.我不会使用“任何”CPU。 While this “may” work, since you are using access x32, then I would force compile your .net class to x86 as opposed to “any” CPU.虽然这“可能”有效,但由于您使用的是 x32 访问权限,因此我会强制将您的 .net 类编译为 x86,而不是“任何”CPU。 This would also spit out some errors in case some of the external assemble(ies) are compared to a particular bit size and thus x86 settings will result in your class not consuming some of the external .net .dll's.如果将某些外部汇编与特定位大小进行比较,这也会产生一些错误,因此 x86 设置将导致您的类不使用某些外部 .net .dll。

Now you “note” that your project works as an .exe, and then “sort of” works as a class and “.dll” registered as a COM.现在您“注意到”您的项目作为 .exe 工作,然后“有点”作为一个类工作,而“.dll”注册为 COM。

However, you don't mention how you are “flipping” this from an .exe to a class, or do you simply have a .net “test” project that you run as an .exe, and it of course has a reference to that class .dll.但是,您没有提到如何将其从 .exe“翻转”到类,或者您是否只是有一个作为 .exe 运行的 .net“测试”项目,它当然有对那个类.dll。 And you NOT using “COM” for this testing.并且您不使用“COM”进行此测试。

If your test.exe program can use that external assembly?如果您的 test.exe 程序可以使用该外部程序集?

Then I would create a test2 project, but this time around do NOT reference the assembly (as .net), but try using .net CreateObject().然后我将创建一个 test2 项目,但这次不要引用程序集(作为 .net),而是尝试使用 .net CreateObject()。 (I think c# has this choice – vb.net certainly does). (我认为 c# 有这个选择——vb.net 当然有)。

So you could try + use a CreateObject() in this .net test sample, and thus test from .net as COM.因此,您可以尝试在此 .net 测试示例中使用 CreateObject(),从而从 .net 作为 COM 进行测试。

So this test .net program (one that has NOT referenced the assembly) would a intermediate test before you jump all the way into ms-access + VBA test code.所以这个测试 .net 程序(一个没有引用程序集的程序)将在你一路跳转到 ms-access + VBA 测试代码之前进行中间测试。

However, I would first test/check if an app.config file is being used here, since as noted when you flip to a COM object, and then create a instance of that object in Access/VBA?但是,我会首先测试/检查这里是否使用了 app.config 文件,因为如上所述,当您翻转到 COM 对象,然后在 Access/VBA 中创建该对象的实例时? Then the config file will NOT be used.那么配置文件将不会被使用。 You can in .net code force/change the app.config file in that running assembly, and I can't seem to find my code example that does this.您可以在 .net 代码中强制/更改该正在运行的程序集中的 app.config 文件,但我似乎找不到执行此操作的代码示例。 But I am not going to dig too hard until such time we confirm that you are using app.exe.config file here.但是在我们确认您在此处使用 app.exe.config 文件之前,我不会深入挖掘。

Edit编辑

Out of cuirous, I googled naudio.出于好奇,我用谷歌搜索了 naudio。 It is managed code.它是托管代码。 So, that library should work just fine.所以,那个库应该可以正常工作。 If that project is compiled as "ANY CPU", you COULD try re-compile as x86, and also re-compile your test program as x86.如果该项目被编译为“任何 CPU”,您可以尝试重新编译为 x86,并将您的测试程序重新编译为 x86。 Then when you create a COM object, then again compile that as x86.然后,当您创建 COM 对象时,再次将其编译为 x86。

As noted, I do quite a bit of COM stuff for ms-access, and I have without issue used Newtonsoft (for jason serializing/de-serializing) as a COM object that is consumed by ms-access.如前所述,我为 ms-access 做了很多 COM 的东西,而且我毫无问题地使用 Newtonsoft(用于 jason 序列化/反序列化)作为 ms-access 使用的 COM 对象。

And given that naudio is managed code then I don't see why it would be an issue.鉴于 naudio 是托管代码,那么我不明白为什么会出现问题。 Any other 3rd party .dll's you using here?您在这里使用任何其他 3rd 方 .dll 吗? As a quick test, I would set all projects to x86.作为快速测试,我将所有项目设置为 x86。 You seem to "mention" that "some" of the COM object works, but just not all.您似乎“提到”COM 对象的“某些”有效,但不是全部。

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

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