如何将此非托管代码从 asp 传输到 asp.net 2/mvc?
我是 ASP.net 互操作功能的新手,所以我这里有一些非托管 dll,我需要从我的 asp.net mvc 应用程序调用它们。
dll 名称为 CTSerialNumChecksum.dll。我可以使用下面的这两行和我可靠的响应轻松地从 asp 调用 dll。写一切工作正常。
set CheckSumObj = Server.CreateObject("CTSerialNumChecksum.CRC32API")
validSno = CheckSumObj.ValidateSerialNumber(no)
我知道它是非托管的,因为当我尝试添加对 dll 的引用时它不起作用。 我尝试遵循一些有关互操作和编组的教程,但到目前为止我无法使代码正常工作。
我试图将对象包装到另一个静态类中,然后让应用程序的其余部分调用代码。所以不知道为什么让它在 ASP.NET 中工作如此困难,或者我只是一个白痴 argggghhh..
using System;
using System.Runtime.InteropServices;
namespace OnlineRegisteration.Models
{
public static class SerialNumberChecksum
{
[DllImport("CTSerialNumChecksum")]
public static extern int ValidateSerialNumber(string serialNo);
}
}
问题:
- 我如何编写该类?我发现的大多数示例都不涉及类、.CRC32API 部分、 以及那些涉及的示例看起来很难
- 我可以用什么工具来识别 特定文件的 dll 类型是什么 是,即非托管c++等?
- 我还打算使用jquery 稍后进行 ajax 调用,以便我可以使用它 验证我预先提交的表格。 有没有更好的方法来处理 这?
Dumpbin 调用返回给我这个,不知道为什么我看不到里面的验证函数名称:
File Type: DLL
Section contains the following exports for CTSerialNumChecksum.DLL
00000000 characteristics
3D75BB53 time date stamp Wed Sep 04 15:50:43 2002
0.00 version
1 ordinal base
4 number of functions
4 number of names
ordinal hint RVA name
1 0 000015CB DllCanUnloadNow
2 1 000015D7 DllGetClassObject
3 2 000015F1 DllRegisterServer
4 3 00001601 DllUnregisterServer
Summary
5000 .data
2000 .rdata
2000 .reloc
2000 .rsrc
8000 .text
更新:
这可能是由于名称修改造成的吗?因为它是一个 c++ dll,至少我怀疑它是......
i'm a newbie to ASP.net interop features, so what i have right here is some unmanaged dll that i need to call from my asp.net mvc app.
the dll name is CTSerialNumChecksum.dll. i can easily call the dll from asp using just those two lines below and from my trusty response. write everything is working fine.
set CheckSumObj = Server.CreateObject("CTSerialNumChecksum.CRC32API")
validSno = CheckSumObj.ValidateSerialNumber(no)
i know it's unmanaged because when i try to add reference to the dll it doesn't work.
i try to follow some tutorials on interop and marshalling but thus far i wasn't able to get the code to work.
i'm trying to wrap the object into another static class and just let the rest of the app to call the code. so not sure why is it so hard to get it working in asp.net or that i'm just an idiot arggghhh..
using System;
using System.Runtime.InteropServices;
namespace OnlineRegisteration.Models
{
public static class SerialNumberChecksum
{
[DllImport("CTSerialNumChecksum")]
public static extern int ValidateSerialNumber(string serialNo);
}
}
Questions:
- How do i write the class? most example i found didn't involve the class, the .CRC32API part, and those that do seems freaky hard
- And what tool can i use to identify
what type of dll a particular file
is, i.e. unmanaged c++, etc? - Also i intend to make use jquery to
do ajax call later so i can use this
to validate my form pre-submission.
Is there a better way to handle
this?
Dumpbin call returned me this, not sure why i can't see the validate function name inside:
File Type: DLL
Section contains the following exports for CTSerialNumChecksum.DLL
00000000 characteristics
3D75BB53 time date stamp Wed Sep 04 15:50:43 2002
0.00 version
1 ordinal base
4 number of functions
4 number of names
ordinal hint RVA name
1 0 000015CB DllCanUnloadNow
2 1 000015D7 DllGetClassObject
3 2 000015F1 DllRegisterServer
4 3 00001601 DllUnregisterServer
Summary
5000 .data
2000 .rdata
2000 .reloc
2000 .rsrc
8000 .text
Updates:
Could this be due to name mangling? as it's a c++ dll, at least what i suspect it to be anyway...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是一个 COM 对象。我可以这么说,因为 ASP 中的 Server.CreateObject() 是实例化 COM 对象的方法。 DLL中的DllRegisterServer等也是明显的标志。
您想要使用.NET中的COM对象。您可以这样做通过在编译时生成互操作程序集,通过运行 tlbimp.exe 工具。基本上,这会自动完成您尝试手动执行的操作。然后使用生成的程序集编译代码。
至于 jQuery 部分 - 我认为你应该把它留给一个完全独立的问题。
ps:我不知道 CRC32API 的作用是什么,但是 有 CRC32 实现在 C# 中可用。您可能会考虑在全托管代码中重新实现该 COM 对象中的任何功能。它将消除编译时对 tlbimp.exe 的需要,以及运行时 COM 对象的注册。
It is a COM object. I can tell because
Server.CreateObject()
in ASP is the way to instantiate a COM object. The DllRegisterServer and so on in the DLL, are also telltale signs.You want to use the COM object from .NET. You can do that by generating an interop assembly, at compile time, by running the tlbimp.exe tool. Basically, this does, automatically, what you were trying to do manually. Then compile your code with the resulting assembly.
As for the jQuery part - I think you should leave that to a completely separate question.
ps: I don't know what that CRC32API thing does, but there are CRC32 implementations available in C#. You might consider re-implementing whatever function is in that COM object, in all-managed code. It would eliminate the need for tlbimp.exe at compile time, and the registration of the COM object at runtime.