简体   繁体   中英

Using a vb6 library from c#

I have a VB application that calls an external dll for address verification. I need to add this code to a C# application but my conversion of the code is not being accepted.

VB6 code:

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:

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:

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.

Perhaps also consider upgrading to the .NET version of the library (NetZipCode v4.4 for .NET - http://www.softwarecompany.com/downloads.html ). The librarires come with sample VB.NET and C# code as well.

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; 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.

You'll need to know what the actual exported C functions are in order make the proper PInvoke method. For example, if the C function for UNZ_GETERRORTEX were defined as such:

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

[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).

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. 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. The user guide says to put them into C:\\Windows\\System but that also does not work.

I found that it suddenly will work if I put Unzdll32.dll in the C:\\Windows\\SysWOW64 directory. 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. But that's OK because I imagine these 2 dll's need to pair up with the matching versions in order to work. Regardless, it now works perfect if Unzdll32.dll is correctly placed (in C:\\Windows\\SysWOW64) on the machine running the application .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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