DLLImport - System.NullReferenceException

J

James

Greetings,

I have a PowerBasic unmanaged library that I wish to call from C#.
Here's the PB code...

FUNCTION Hello(str AS STRING) EXPORT AS STRING
FUNCTION = "Hello World " & str
END FUNCTION

Simple enough. I can successfully call it from a VB6 component but
can't seem to get the C# syntax right. I've tried the following with
no success. Each time, I get this error at the call to HELLO. Any
ideas?

System.NullReferenceException: Object reference not set to an instance
of an object.

**Attempt 1**
[DllImport("Hello.dll", EntryPoint="HELLO")]
public static extern string HELLO(string someText);
Console.WriteLine(HELLO("blah"));

**Attempt 2**
[DllImport("Hello.dll", EntryPoint="HELLO", CharSet=CharSet.Auto)]
public static extern string HELLO(ref string someText);
string str = "blah";
Console.WriteLine(HELLO(ref str));

**Attempt 3**
[DllImport("Hello.dll", EntryPoint="HELLO")]
public static extern string HELLO([MarshalAs(UnmanagedType.LPStr)]
string someText);
Console.WriteLine(HELLO("blah"));

**Attempt 4**
[DllImport("Hello.dll", EntryPoint="HELLO")]
public static extern string HELLO(StringBuilder someText);
StringBuilder sb = new StringBuilder("blah");
Console.WriteLine(HELLO(sb));

-Cliff
 
M

Mohamoss

Hi
where is Hello.dll, since it is not a system file locate it into the exe
directory
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
J

James

Hi,
Yes, Hello.dll and the exe are in the same directory (..\bin\debug).
I've tried moving it to other folders with the same results.
What else could cause an issue here? I'm stumped.
-Cliff
 
M

Mohamoss

HI
You need to debug your program and step into each statement to get
exactly where this exception occurs , this will help a lot since there is
no obvious reason we can see now
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 

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