Unicode in a native DLL?

O

ORC

Hi,

How to transfer strings between a C# .NET application and a native DLL that
uses Unicode like:
#ifndef UNICODE
#define UNICODE
#endif

The function header in the DLL:
BOOL NewDataBase(LPCTSTR strDbFileName)

The definition in C#:
[DllImport("miniDataservice.dll", EntryPoint="NewDataBase",
SetLastError=true)]
public static extern bool NewDataBase(string FileName);

Thanks,
Ole
 
O

ORC

HI,
saw the moment I pressed the send button: I forgot the charset option:
[DllImport("miniDataservice.dll", EntryPoint="NewDataBase",
SetLastError=true, CharSet = CharSet.Unicode)]

Now it works!

Thanks anyway,
Ole
 
G

GingerDeafMan

Hi Ole and the rest,

Have just subscribed to this NG and found this. I have a similar
problem.

I am trying to get a function in C++ DLL to return a string.

Code in DLL is (a portion)

#define WINDLL __declspec(dllexport) CALLBACK
void WINDLL GetName(LPCTSTR szMfr)
{
sprintf((char *)szMfr,"%s",A.Name);
}

"Name" is char array.

Code in C# is

[DllImport("C:\\MyDLL.DLL",
EntryPoint="GetName",CharSet=CharSet.Auto)]
private static extern void GetName(string strName);

This throws an exception

string strName;
GetName(strName);

I am stuck here, obviously. Any ideas as what I am doing wrong, or
alternative methods for passing char arrays back from the DLL to C# as a
string would be welcome.

TIA,

Cheers,

Paul.


HI,
saw the moment I pressed the send button: I forgot the charset option:
[DllImport("miniDataservice.dll", EntryPoint="NewDataBase",
SetLastError=true, CharSet = CharSet.Unicode)]

Now it works!

Thanks anyway,
Ole

ORC said:
Hi,

How to transfer strings between a C# .NET application and a native DLL that
uses Unicode like:
#ifndef UNICODE
#define UNICODE
#endif

The function header in the DLL:
BOOL NewDataBase(LPCTSTR strDbFileName)

The definition in C#:
[DllImport("miniDataservice.dll", EntryPoint="NewDataBase",
SetLastError=true)]
public static extern bool NewDataBase(string FileName);

Thanks,
Ole
 
W

Willy Denoyette [MVP]

What exception?
Your function is expecting an ANSI string, so you should use CharSet.Ansi
And you don't return anything.

Willy.

GingerDeafMan said:
Hi Ole and the rest,

Have just subscribed to this NG and found this. I have a similar
problem.

I am trying to get a function in C++ DLL to return a string.

Code in DLL is (a portion)

#define WINDLL __declspec(dllexport) CALLBACK
void WINDLL GetName(LPCTSTR szMfr)
{
sprintf((char *)szMfr,"%s",A.Name);
}

"Name" is char array.

Code in C# is

[DllImport("C:\\MyDLL.DLL",
EntryPoint="GetName",CharSet=CharSet.Auto)]
private static extern void GetName(string strName);

This throws an exception

string strName;
GetName(strName);

I am stuck here, obviously. Any ideas as what I am doing wrong, or
alternative methods for passing char arrays back from the DLL to C# as a
string would be welcome.

TIA,

Cheers,

Paul.


HI,
saw the moment I pressed the send button: I forgot the charset option:
[DllImport("miniDataservice.dll", EntryPoint="NewDataBase",
SetLastError=true, CharSet = CharSet.Unicode)]

Now it works!

Thanks anyway,
Ole

ORC said:
Hi,

How to transfer strings between a C# .NET application and a native DLL that
uses Unicode like:
#ifndef UNICODE
#define UNICODE
#endif

The function header in the DLL:
BOOL NewDataBase(LPCTSTR strDbFileName)

The definition in C#:
[DllImport("miniDataservice.dll", EntryPoint="NewDataBase",
SetLastError=true)]
public static extern bool NewDataBase(string FileName);

Thanks,
Ole
 
G

GingerDeafMan

Hi Willy,

Thanks for this.

Have already tried CharSet.Ansi. Makes no difference.

The exception is

An unhandled exception of type 'System.NullReferenceException' occurred
in nch.dll

Additional information: Object reference not set to an instance of an
object

Paul.

What exception?
Your function is expecting an ANSI string, so you should use CharSet.Ansi
And you don't return anything.

Willy.

GingerDeafMan said:
Hi Ole and the rest,

Have just subscribed to this NG and found this. I have a similar
problem.

I am trying to get a function in C++ DLL to return a string.

Code in DLL is (a portion)

#define WINDLL __declspec(dllexport) CALLBACK
void WINDLL GetName(LPCTSTR szMfr)
{
sprintf((char *)szMfr,"%s",A.Name);
}

"Name" is char array.

Code in C# is

[DllImport("C:\\MyDLL.DLL",
EntryPoint="GetName",CharSet=CharSet.Auto)]
private static extern void GetName(string strName);

This throws an exception

string strName;
GetName(strName);

I am stuck here, obviously. Any ideas as what I am doing wrong, or
alternative methods for passing char arrays back from the DLL to C# as a
string would be welcome.

TIA,

Cheers,

Paul.


HI,
saw the moment I pressed the send button: I forgot the charset option:
 
W

Willy Denoyette [MVP]

But what is nch.dll?
Your exception is a managed exception indicating a null reference usage.
Need some more code to investigate the problem, please post at least the
code part surrounding the intruction/call that throws.
Also when you intend to pass and return a char* you should pass a
StringBuilder instead of a String
Willy.


GingerDeafMan said:
Hi Willy,

Thanks for this.

Have already tried CharSet.Ansi. Makes no difference.

The exception is

An unhandled exception of type 'System.NullReferenceException' occurred
in nch.dll

Additional information: Object reference not set to an instance of an
object

Paul.

What exception?
Your function is expecting an ANSI string, so you should use CharSet.Ansi
And you don't return anything.

Willy.

GingerDeafMan said:
Hi Ole and the rest,

Have just subscribed to this NG and found this. I have a similar
problem.

I am trying to get a function in C++ DLL to return a string.

Code in DLL is (a portion)

#define WINDLL __declspec(dllexport) CALLBACK
void WINDLL GetName(LPCTSTR szMfr)
{
sprintf((char *)szMfr,"%s",A.Name);
}

"Name" is char array.

Code in C# is

[DllImport("C:\\MyDLL.DLL",
EntryPoint="GetName",CharSet=CharSet.Auto)]
private static extern void GetName(string strName);

This throws an exception

string strName;
GetName(strName);

I am stuck here, obviously. Any ideas as what I am doing wrong, or
alternative methods for passing char arrays back from the DLL to C# as
a
string would be welcome.

TIA,

Cheers,

Paul.


HI,
saw the moment I pressed the send button: I forgot the charset
option:
 
G

GingerDeafMan

nch.dll should have been mydll.dll. They are one and the same. I
simply forgot to rename nch.dll to mydll.dll to match the earlier
posting.

The code that causes the exception is this line

GetManufacturerName(_Name);

_Name is a string.

I am not sure what "null reference usage" means.

Ta for the tip re StringBuilder. Will investigate this.

Paul.
 
G

GingerDeafMan

GetManufacturerName(_Name) should have been GetName(strName), to follow
the earlier example. Today is definitely not one of my better days :O

But I think we are getting the idea.

GetName(strName) is throwing the exception and I don't know what to do
about this.

Have just tried StringBuilder but still this makes no difference (same
exception thrown).

Paul.
 

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