1

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.

1
  • 3
    you cannot and don't need add those dlls as c# reference Commented Jul 17, 2017 at 1:25

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 (e.g. in the same folder).

Hope that can help.

3 Comments

Thank you txtechhelp, I was working in the code and researching and finally found the same answer. However; while I was able to add the code as suggested above, I am having an issue getting the results which are modified in the return statement. UNZ_CHECKADDRESS - returns a value to indicate the address doesn't match in which case I can call UNZ_GETSTDADDRESS which will give me the matching addresses. You have it as a "void" return, will that allow me to pull the values from the string variables in the parentheses? Thanks. :)
Get unique ID: long hUnz = UNZ_INIT_EX(); Check Address: long ResultCode = UNZ_CHECKADDRESS(hUnz, " ", " ", "real street", "city, state zip"); If result is <> 0 then: UNZ_GETSTDADDRESS(hUnz, szFirmName, szPRUrb, szDelLine, szLastLine); // The new address(es) are put into the variables in VB.
@user8316770; the UNZ_GETSTDADDRESS is declared void because the VB code has it as Sub, which has no return, thus void in C#. And as stated in the answer, you'll have to add some qualifiers (like out or ref) based on what the C function call wants in the DLL .. C# isn't as forgiving as VB when it comes to stuff like this
0

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 .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.