accessing unmanaged DLL in C#

  • Thread starter delphiconsultingguy
  • Start date
D

delphiconsultingguy

Hello,

I'm trying to convert a VB.Net app into C#. The VB app calls functions
in an unmanaged DLL. I'm able to call most of the functions in the DLL
from C# with the exception of the one below:

in VB.Net, the unmanaged function is declared through an interop as:

Public Overridable Function LocGetName(ByVal pLoc As Integer, ByVal
pLocNum As Integer, ByVal pType As LOCOLELib.tLocNameType, ByVal
pFullName As Boolean, ByRef pName As String, ByVal pMaxSize As Integer)
As String

and I've declared it in C# like:

[DllImport("Location.dll")]
static extern string LocGetName(int location, int locationNumber,
locationNameType locationNameType, bool fullName, ref string name, int
maxSize);

when I run the following statement in VB.Net, it works fine:

LocOle.LocGetName(llLocations, llLocIndex,
LOCOLELib.tLocNameType.eLocNameState, True, lsStateName.Value,
LOCOLELib.tLocOleConstants.eLOC_NAME_STATE_LEN)

but when I run the following statement in C#, I get 'Object reference
not set to an instance of an object':

LocGetName(locations, i, locationNameType.state, true, ref stateString,
MAX_STATE_LENGTH);

Every parameter sent is an integer with the exception of the 'true',
and the statestring. I've tried initializing the stateString to some
text, I've tried changing the parameter type to 'out', and pass by
value, to no avail. I've even tried declaring my stateString with a
marshaling attribute:

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_STATE_LENGTH)]
string stateString = "";

and that didn't work. I also tried declaring stateString as a
StringBuilder object (changing the function parameter types
accordingly) and that didn't work. Is there anything else I'm missing?
thanks,
Sean
 
M

Mattias Sjögren

in VB.Net, the unmanaged function is declared through an interop as:

Public Overridable Function LocGetName(ByVal pLoc As Integer, ByVal
pLocNum As Integer, ByVal pType As LOCOLELib.tLocNameType, ByVal
pFullName As Boolean, ByRef pName As String, ByVal pMaxSize As Integer)
As String

That can't be a VB.NET external function declaration. For that you
either the Declare statement, or have a Shared method decorated with
the DllImport attribute, and Overridable can't be used on shared
methods. How about posting the real declaration?




Mattias
 
D

delphiconsultingguy

Public Overridable Function LocGetName(ByVal pLoc As Integer, ByVal
That can't be a VB.NET external function declaration. For that you
either the Declare statement, or have a Shared method decorated with
the DllImport attribute, and Overridable can't be used on shared
methods. How about posting the real declaration?

The program was originally in old VB, and when I loaded it into Visual
Studio .NET, it was morphed into a .NET version that utilized a newly
created Interop dll. The contents of this Interop dll can be viewed in
the object browser, and by navigating its tree I was able to find the
LocGetName function and it was described at the bottom of the window
exactly how I wrote it above.

Sean
 
M

Mattias Sjögren

The program was originally in old VB, and when I loaded it into Visual
Studio .NET, it was morphed into a .NET version that utilized a newly
created Interop dll.

I'm not that familiar with the VB upgrade wizard, but I though it only
created interop assemblies for COM components you reference, not
Declare statements. Is the library you're calling a COM library or
does it export static entrypoints?



Mattias
 
D

delphiconsultingguy

I'm not that familiar with the VB upgrade wizard, but I though it
only
created interop assemblies for COM components you reference, not
Declare statements. Is the library you're calling a COM library or
does it export static entrypoints?

It is an interop assembly that is generated; there are no declare
statements. I pulled that function's definition from the object
browser but it's not in code anywhere.
 
M

Mattias Sjögren

It is an interop assembly that is generated; there are no declare
statements. I pulled that function's definition from the object
browser but it's not in code anywhere.

OK, then you should't write any DllImport methods at all. Just
reference the interop assembly generated for the VB project.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top