简体   繁体   English

使用C#中的vb6库

[英]Using a vb6 library from c#

I have a VB application that calls an external dll for address verification. 我有一个VB应用程序,该应用程序调用外部dll进行地址验证。 I need to add this code to a C# application but my conversion of the code is not being accepted. 我需要将此代码添加到C#应用程序中,但是我的代码转换未被接受。

VB6 code: VB6代码:

Declare Function UNZ_INIT_EX Lib "UNZDLL32.DLL" () As Long

Declare Function UNZ_TERM Lib "UNZDLL32.DLL" (ByVal hUnz As Long) As Long

Declare Function UNZ_CHECKADDRESS Lib "UNZDLL32.DLL" (ByVal hUnz As Long, ByVal Line1$, ByVal line2$, ByVal line3$, ByVal Line4$) As Long

Declare Sub UNZ_GETSTDADDRESS Lib "UNZDLL32.DLL" (ByVal hUnz As Long, ByVal szFirmName As String, ByVal szPRUrb As String, ByVal szDelLine As String, ByVal szLastLine As String)

Declare Sub UNZ_GETERRORTEXT Lib "UNZDLL32.DLL" (ByVal hUnz As Long, ByVal ErrorText As String)

Declare Function UNZ_GETMATCHCOUNT Lib "UNZDLL32.DLL" (ByVal hUnz As Long) As Long

Declare Sub UNZ_GETMATCHADDR Lib "UNZDLL32.DLL" (ByVal hUnz As Long, ByVal intItem As Integer, ByVal szFirmName As String, ByVal szPRUrb As String, ByVal szDelLine As String, ByVal szLastLine As String)

Declare Sub UNZ_GETAREACODE Lib "UNZDLL32.DLL" (ByVal hUnz As Long, ByVal szAreaCode As String)

C# code: C#代码:

using System.Runtime.InteropServices;
[DllImport("unzdll32.dll", CharSet.Auto)]

And I started doing this: 我开始这样做:

public static extern long UNZ_INIT_EX();
public static extern long UNZ_TERM(long hUnz);

But, I am unable to add this DLL as a reference due to this error: 但是,由于此错误,我无法添加此DLL作为参考:

Error: A reference to ... could not be added. 错误:无法添加对...的引用。 Please make sure that the file is accessible, and that it is a valid assembly or COM component. 请确保该文件可访问,并且它是有效的程序集或COM组件。

Perhaps also consider upgrading to the .NET version of the library (NetZipCode v4.4 for .NET - http://www.softwarecompany.com/downloads.html ). 也许还考虑升级到该库的.NET版本(用于.NET的NetZipCode v4.4- http://www.softwarecompany.com/downloads.html )。 The librarires come with sample VB.NET and C# code as well. 这些库也附带示例VB.NET和C#代码。

Try this: 尝试这个:

[DllImport("UNZDLL32.DLL")]
public static extern long UNZ_INIT_EX();

[DllImport("UNZDLL32.DLL")]
public static extern long UNZ_TERM (long hUnz);

[DllImport("UNZDLL32.DLL", CharSet.Auto)]
public static extern long UNZ_CHECKADDRESS(long hUnz, string Line1, string line2, string line3, string Line4);

[DllImport("UNZDLL32.DLL", CharSet.Auto)]
public static extern void UNZ_GETSTDADDRESS(long hUnz, string szFirmName, string szPRUrb, string szDelLine, string szLastLine);

[DllImport("UNZDLL32.DLL", CharSet.Auto)]
public static extern void UNZ_GETERRORTEXT(long hUnz, string ErrorText);

[DllImport("UNZDLL32.DLL")]
public static extern long UNZ_GETMATCHCOUNT(long hUnz);

[DllImport("UNZDLL32.DLL", CharSet.Auto)]
public static extern void UNZ_GETMATCHADDR(long hUnz, int intItem, string szFirmName, string szPRUrb, string szDelLine, string szLastLine);

[DllImport("UNZDLL32.DLL", CharSet.Auto)]
public static extern void UNZ_GETAREACODE(long hUnz, string szAreaCode);

That's a direct VB to C# DllImport of your code; 这是直接将VB DllImport您的代码的C# DllImport it should be noted, however, that the types or identifiers might actually need to be changed/added. 但是应注意,实际上可能需要更改/添加类型或标识符。 For example, a long in .NET is a signed 64-bit type, and while it's big enough to hold a 32-bit type, it might cause issues with your DLL if the types need to match widths. 例如,.NET中的long是带符号的64位类型,虽然它足够大以容纳32位类型,但是如果类型需要匹配宽度,则它可能会导致DLL问题。

You'll need to know what the actual exported C functions are in order make the proper PInvoke method. 您需要知道实际导出的C函数是什么,才能进行正确的PInvoke方法。 For example, if the C function for UNZ_GETERRORTEX were defined as such: 例如,如果将UNZ_GETERRORTEX的C函数定义为:

DLL_EXPORT void UNZ_GETERRORTEXT(long hUnz, _Out_opt_ LPTSTR ErrorText);
// or
DLL_EXPORT void UNZ_GETERRORTEXT(long hUnz, char* ErrorText)

Then the C# could be defined like so 然后可以像这样定义C#

[DllImport("UNZDLL32.DLL", CharSet = CharSet.Auto)]
public static extern void UNZ_GETERRORTEXT(long hUnz, StringBuilder ErrorText);
// or
[DllImport("UNZDLL32.DLL", CharSet = CharSet.Auto)]
public static extern void UNZ_GETERRORTEXT(long hUnz, ref string ErrorText);`

You'll also need to make sure the UNZDLL32.DLL is somewhere it can be accessed by your application (eg in the same folder). 您还需要确保UNZDLL32.DLL位于您的应用程序可以访问的位置(例如,在同一文件夹中)。

Hope that can help. 希望能对您有所帮助。

I use the same program. 我使用相同的程序。 I have a 64-bit machine but have only been successful loading NetZipCode.dll and not NetZipCode64.dll. 我有一台64位计算机,但仅成功加载NetZipCode.dll,而不是NetZipCode64.dll。 I am able to add reference to NetZipCode.dll and all the Fujitso files, but get the same error as you when trying to add reference to Unzdll32.dll or Unzdll64.dll. 我能够添加对NetZipCode.dll和所有Fujitso文件的引用,但是在尝试添加对Unzdll32.dll或Unzdll64.dll的引用时遇到与您相同的错误。 The user guide says to put them into C:\\Windows\\System but that also does not work. 用户指南说将它们放入C:\\ Windows \\ System,但这也不起作用。

I found that it suddenly will work if I put Unzdll32.dll in the C:\\Windows\\SysWOW64 directory. 我发现如果将Unzdll32.dll放在C:\\ Windows \\ SysWOW64目录中,它将突然起作用。 As mentioned, I am using NetZipCode.dll on a 64 bit machine, so basically, I am only successful using the 32 bit version for everything. 如前所述,我在64位计算机上使用NetZipCode.dll,因此,基本上,我仅对所有内容成功使用32位版本。 But that's OK because I imagine these 2 dll's need to pair up with the matching versions in order to work. 但这没关系,因为我想这2个dll需要与匹配的版本配对才能工作。 Regardless, it now works perfect if Unzdll32.dll is correctly placed (in C:\\Windows\\SysWOW64) on the machine running the application . 无论如何,如果将Unzdll32.dll正确地放置在运行该应用程序的计算机上(位于C:\\ Windows \\ SysWOW64中),它现在可以完美地工作。

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

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