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
 
D

delphiconsultingguy

I believe the fact that I'm converting from VB.NET to C# would qualify
it for this newsgroup.
 

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