Unmanaged code(dll) function: int myfunc (char* temp)

G

Guest

Hi,
I want to do an unmanaged call to an DLL
Unmanaged code(dll) function: int myfunc (char* temp)
Here return int type determines the Success.
After execution of the function temp will contain an value

How to do the unmanaged call to this function from c# and later how to
retrieve the value from temp, which hold a string value.

thanks in Advance,
Murthy
 
G

Guest

First thing to know is that CE doesn't store strings in a char* type, it
uses UTF16, so they are wchar_t.

If the native DLL will be putting data into that variable, you need to
allocate enough memory for it before the call. The easiest way is with a
StringBuilder.

int myfunc (TCHAR* temp)
{
.....
}

[DllImport("mylib.dll")]
public static extern int myfunc(StringBuilder sb);

void MyCall()
{
StringBuilder sb = new StringBuilder(255);
int ret = myfunc(sb);
}
 
G

Guest

Hi,
Thanks for ur Reply....
I tried StringBuilder it worked, But there is some problem with conversion i
think.
When i tried to convert it to string as sb.Tostring(), it is giving me some
junk characters which are not alphanumeric.

Can u tell me what i need to do to convert the stringbuilder sb to some
alphanumeric string.

Murthy.

First thing to know is that CE doesn't store strings in a char* type, it
uses UTF16, so they are wchar_t.

If the native DLL will be putting data into that variable, you need to
allocate enough memory for it before the call. The easiest way is with a
StringBuilder.

int myfunc (TCHAR* temp)
{
.....
}

[DllImport("mylib.dll")]
public static extern int myfunc(StringBuilder sb);

void MyCall()
{
StringBuilder sb = new StringBuilder(255);
int ret = myfunc(sb);
}


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Murthy said:
Hi,
I want to do an unmanaged call to an DLL
Unmanaged code(dll) function: int myfunc (char* temp)
Here return int type determines the Success.
After execution of the function temp will contain an value

How to do the unmanaged call to this function from c# and later how to
retrieve the value from temp, which hold a string value.

thanks in Advance,
Murthy
 
G

Guest

Without seeing the code in your C method, there's no way to know what's
wrong.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Murthy said:
Hi,
Thanks for ur Reply....
I tried StringBuilder it worked, But there is some problem with conversion
i
think.
When i tried to convert it to string as sb.Tostring(), it is giving me
some
junk characters which are not alphanumeric.

Can u tell me what i need to do to convert the stringbuilder sb to some
alphanumeric string.

Murthy.

First thing to know is that CE doesn't store strings in a char* type, it
uses UTF16, so they are wchar_t.

If the native DLL will be putting data into that variable, you need to
allocate enough memory for it before the call. The easiest way is with a
StringBuilder.

int myfunc (TCHAR* temp)
{
.....
}

[DllImport("mylib.dll")]
public static extern int myfunc(StringBuilder sb);

void MyCall()
{
StringBuilder sb = new StringBuilder(255);
int ret = myfunc(sb);
}


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Murthy said:
Hi,
I want to do an unmanaged call to an DLL
Unmanaged code(dll) function: int myfunc (char* temp)
Here return int type determines the Success.
After execution of the function temp will contain an value

How to do the unmanaged call to this function from c# and later how to
retrieve the value from temp, which hold a string value.

thanks in Advance,
Murthy
 
G

Guest

C++ code:

int myfunc(char* temp)
{
//Assigning an value to temp
strcpy(temp,L"abcd");

}
I am using char* not TCHAR*

Can you help me how to get the value of temp when using string builder
from managed code



Without seeing the code in your C method, there's no way to know what's
wrong.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Murthy said:
Hi,
Thanks for ur Reply....
I tried StringBuilder it worked, But there is some problem with conversion
i
think.
When i tried to convert it to string as sb.Tostring(), it is giving me
some
junk characters which are not alphanumeric.

Can u tell me what i need to do to convert the stringbuilder sb to some
alphanumeric string.

Murthy.

First thing to know is that CE doesn't store strings in a char* type, it
uses UTF16, so they are wchar_t.

If the native DLL will be putting data into that variable, you need to
allocate enough memory for it before the call. The easiest way is with a
StringBuilder.

int myfunc (TCHAR* temp)
{
.....
}

[DllImport("mylib.dll")]
public static extern int myfunc(StringBuilder sb);

void MyCall()
{
StringBuilder sb = new StringBuilder(255);
int ret = myfunc(sb);
}


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Hi,
I want to do an unmanaged call to an DLL
Unmanaged code(dll) function: int myfunc (char* temp)
Here return int type determines the Success.
After execution of the function temp will contain an value

How to do the unmanaged call to this function from c# and later how to
retrieve the value from temp, which hold a string value.

thanks in Advance,
Murthy
 
J

José Miguel Torres

Hi and sorry for the interference.

As Chris is saying, char* is 8bit ANSI character based. Try using TCHAR*
(16bit UNICODE). Perhaps your problem resides on native library.

regards,


--
José Miguel Torres
jtorres_diaz~~ARROBA~~terra.es
http://jmtorres.blogspot.com
http://www.desarrolloMobile.NET


Murthy said:
C++ code:

int myfunc(char* temp)
{
//Assigning an value to temp
strcpy(temp,L"abcd");

}
I am using char* not TCHAR*

Can you help me how to get the value of temp when using string builder
from managed code



Without seeing the code in your C method, there's no way to know what's
wrong.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Murthy said:
Hi,
Thanks for ur Reply....
I tried StringBuilder it worked, But there is some problem with
conversion
i
think.
When i tried to convert it to string as sb.Tostring(), it is giving me
some
junk characters which are not alphanumeric.

Can u tell me what i need to do to convert the stringbuilder sb to some
alphanumeric string.

Murthy.

:

First thing to know is that CE doesn't store strings in a char* type,
it
uses UTF16, so they are wchar_t.

If the native DLL will be putting data into that variable, you need to
allocate enough memory for it before the call. The easiest way is
with a
StringBuilder.

int myfunc (TCHAR* temp)
{
.....
}

[DllImport("mylib.dll")]
public static extern int myfunc(StringBuilder sb);

void MyCall()
{
StringBuilder sb = new StringBuilder(255);
int ret = myfunc(sb);
}


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Hi,
I want to do an unmanaged call to an DLL
Unmanaged code(dll) function: int myfunc (char* temp)
Here return int type determines the Success.
After execution of the function temp will contain an value

How to do the unmanaged call to this function from c# and later how
to
retrieve the value from temp, which hold a string value.

thanks in Advance,
Murthy
 
G

Guest

Rewrite the C library to use TCHAR, since CE is heavily biased that way. If
you *must* use char (and the example makes me doubt it), then you'll have to
bring it back as a byte[] and use the Encoding.ASII class to convert it to a
string.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



Murthy said:
C++ code:

int myfunc(char* temp)
{
//Assigning an value to temp
strcpy(temp,L"abcd");

}
I am using char* not TCHAR*

Can you help me how to get the value of temp when using string builder
from managed code



Without seeing the code in your C method, there's no way to know what's
wrong.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Murthy said:
Hi,
Thanks for ur Reply....
I tried StringBuilder it worked, But there is some problem with
conversion
i
think.
When i tried to convert it to string as sb.Tostring(), it is giving me
some
junk characters which are not alphanumeric.

Can u tell me what i need to do to convert the stringbuilder sb to some
alphanumeric string.

Murthy.

:

First thing to know is that CE doesn't store strings in a char* type,
it
uses UTF16, so they are wchar_t.

If the native DLL will be putting data into that variable, you need to
allocate enough memory for it before the call. The easiest way is
with a
StringBuilder.

int myfunc (TCHAR* temp)
{
.....
}

[DllImport("mylib.dll")]
public static extern int myfunc(StringBuilder sb);

void MyCall()
{
StringBuilder sb = new StringBuilder(255);
int ret = myfunc(sb);
}


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Hi,
I want to do an unmanaged call to an DLL
Unmanaged code(dll) function: int myfunc (char* temp)
Here return int type determines the Success.
After execution of the function temp will contain an value

How to do the unmanaged call to this function from c# and later how
to
retrieve the value from temp, which hold a string value.

thanks in Advance,
Murthy
 

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