473,378 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

accessing unmanaged DLL in C#

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

Nov 21 '05 #1
2 1228
This is a VB.NET forum & not a C# one

"de*****************@yahoo.com" wrote:
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

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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Farmer | last post by:
I am having problems accessing a managed object from unmanaged code. Specifically, I have a .NET forms application with a web service proxy. The forms application links to an unmanaged DLL which...
2
by: Brett | last post by:
We have an application completely written in C++ under visual studio v6. One of our customers wants to use some of our existing components in his project but doesn't know C++ and isn't interested...
2
by: Brett Styles | last post by:
Hi Guys, I am trying to access a class in an unmanaged dll. I have created a wrapper managed class to access the functions I need but no matter what I try from the MSDN samples I can not get it to...
1
by: sklett | last post by:
STILL trying to wrap an unmanaged C++ class that is itself a wrapper to some COM stuff, not sure, it is littered with LPDISPATCH and InvokeHelper, etc. Problem is, when something goes wrong, I'm...
4
by: Bae,Hyun-jik | last post by:
Hi, What is the best case for accessing managed code from unmanaged C++ code, considering that the unmanaged code doesn't have managed extension? Please reply. Thanks in advance. Regards,...
3
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object...
1
by: Stephan Zaubzer | last post by:
Hi I relatively new to C# and at the moment I am having troubles accessing com objects within C#. I am working in VS.net. I add the com library I want to access to my references. Accessing...
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
1
by: omantawy | last post by:
Hi, I have some legacy ASP web applications that use an unmanaged COM component to connect to a third party application. The third part application has moved to the managed code in the current...
4
by: Mau | last post by:
Hi, I have a managed dll which I am accessing from an unmanaged exe using Com Interop. I installed everything on computer 1 and run the unmanaged exe and from exe called my managed dll and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.