473,703 Members | 3,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with importing unmanaged code

Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I
wrote a simple function which takes in 2 buffers (one a byte buffer, one a
char buffer) and copies the contents of the byte buffer into the character
pointer. The code looks like the following:

#include <stdio.h>
#include <stdlib.h>
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_c all,
LPVOID lpReserved
)
{
return TRUE;
}

extern "C" __declspec(dlle xport)
void ReturnCharPoint er(UINT8 *buffer, int buffersize, char *charbuf, int
charbufsize) {
int i =0;
for(i=0; i < charbufsize; i++) {
if(i>=buffersiz e)
return;
//printf("BYTE %d: %d\r\n",i,buffe r[i]);
charbuf[i] = buffer[i];
//printf("CHAR %d: %c\r\n",i,charb uf[i]);
}
}

Now I try to import that function with the following line in a C# project:

[DllImport("DllS hell")]
unsafe static extern void ReturnCharPoint er(byte *buffer, int buffersize,
char *charbuf, int charbufsize);

I use this in the following function:

unsafe private void ParseBuffer()
{
int a = 100;
int b = 75;
byte[] bytebuf = new byte[a];
char[] charbuf = new char[b];
for(int i =0; i <a; i++)
{
bytebuf[i]= Convert.ToByte( i);
}
fixed(byte *bbuf=bytebuf)
fixed(char *cbuf = charbuf)
ReturnCharPoint er(bbuf,a,cbuf, b);
for(int i =0; i < b; i++)
{
Console.Write(c harbuf[i] + ", ");
}

}

My problem is the following: I can import and run the code just fine.
However, when the function exits, the data in the character buffer is not
what is expected! The byte buffer contains the numbers 0-99, the character
buffer, whose size is 75 contains the following : [0] = 256, [1] = 770, [2]
= 1284, [3] = 1798.... etc. The rest of the array is incremented at the same
rate until index 37 which contains the value "74". Since 74 should be the
last entry in the buffer, i suspect that the data is somehow being squashed
on the way into or out of the unmanaged C dll. Also I was unable to use
printf in the dll as it threw the following compiler error:
"error C3861: 'printf': identifier not found, even with argument-dependent
lookup"

If anyone has any clue as to what is going on, it would be greatly
appreciated!!

Thanks,
Tim


Nov 15 '05 #1
7 3304

Hi Timothy,

Normally, there are 2 ways of interop with unmanaged dll, safe way and
unsafe way.
While the unsafe way is not recommanded, because its memory operations are
all not safe.
Here is the safe way of consuming the unmanaged dll:

[DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
static extern void ReturnCharPoint er( byte []bytebuf , int buffersize,
IntPtr charbuf, int charbufsize);

int a = 100;
int b = 75;
byte[] bytebuf = new byte[a];
IntPtr charbuf = Marshal.AllocHG lobal(b);

for(int i =0; i <a; i++)
{
bytebuf[i]= Convert.ToByte( i);
}
ReturnCharPoint er(bytebuf,a,ch arbuf,b);

for(int k=0;k<b;k++)
{
Console.WriteLi ne(Marshal.Read Byte(charbuf,k) );
}

In the last for loop, I use Marshal.Read* function to retrieve data from
unmanaged memory. You can also create a char array in managed memory and
use Marshal.Copy method to copy the unmanaged memory to managed memory,
then it will be comfortable to use.

If you still want to use unsafe way to interop the unmanaged dll, you
should alloc the memory from unmanaged memory, but your code alloc from the
managed memory.
You can do like this:
[DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
unsafe static extern void ReturnCharPoint er( byte *bytebuf , int
buffersize, char * charbuf, int charbufsize);

int a = 100;
int b = 75;

byte* bytebuf=(byte*) Marshal.AllocCo TaskMem(a);
char* charbuf=(char*) Marshal.AllocCo TaskMem(b);

for(int i =0; i <a; i++)
{
bytebuf[i]= Convert.ToByte( i);
}

ReturnCharPoint er( bytebuf,a, charbuf,b);

After ReturnCharPoint er method, in the memory window, you can see that the
charbuf is what you want, but if you want to show it out, you should
interop with some umanaged function such as printf, MessageBoxA to show it
out.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Timothy Shih" <ts***@sensicas t.com>
| Subject: Need help with importing unmanaged code
| Date: Mon, 13 Oct 2003 11:08:15 -0400
| Lines: 78
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eN************ **@tk2msftngp13 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net 66.92.93.226
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1909 96
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I
| wrote a simple function which takes in 2 buffers (one a byte buffer, one a
| char buffer) and copies the contents of the byte buffer into the character
| pointer. The code looks like the following:
|
| #include <stdio.h>
| #include <stdlib.h>
| #include "stdafx.h"
| BOOL APIENTRY DllMain( HANDLE hModule,
| DWORD ul_reason_for_c all,
| LPVOID lpReserved
| )
| {
| return TRUE;
| }
|
| extern "C" __declspec(dlle xport)
| void ReturnCharPoint er(UINT8 *buffer, int buffersize, char *charbuf, int
| charbufsize) {
| int i =0;
| for(i=0; i < charbufsize; i++) {
| if(i>=buffersiz e)
| return;
| //printf("BYTE %d: %d\r\n",i,buffe r[i]);
| charbuf[i] = buffer[i];
| //printf("CHAR %d: %c\r\n",i,charb uf[i]);
| }
| }
|
| Now I try to import that function with the following line in a C# project:
|
| [DllImport("DllS hell")]
| unsafe static extern void ReturnCharPoint er(byte *buffer, int buffersize,
| char *charbuf, int charbufsize);
|
| I use this in the following function:
|
| unsafe private void ParseBuffer()
| {
| int a = 100;
| int b = 75;
| byte[] bytebuf = new byte[a];
| char[] charbuf = new char[b];
| for(int i =0; i <a; i++)
| {
| bytebuf[i]= Convert.ToByte( i);
| }
| fixed(byte *bbuf=bytebuf)
| fixed(char *cbuf = charbuf)
| ReturnCharPoint er(bbuf,a,cbuf, b);
| for(int i =0; i < b; i++)
| {
| Console.Write(c harbuf[i] + ", ");
| }
|
| }
|
| My problem is the following: I can import and run the code just fine.
| However, when the function exits, the data in the character buffer is not
| what is expected! The byte buffer contains the numbers 0-99, the character
| buffer, whose size is 75 contains the following : [0] = 256, [1] = 770,
[2]
| = 1284, [3] = 1798.... etc. The rest of the array is incremented at the
same
| rate until index 37 which contains the value "74". Since 74 should be the
| last entry in the buffer, i suspect that the data is somehow being
squashed
| on the way into or out of the unmanaged C dll. Also I was unable to use
| printf in the dll as it threw the following compiler error:
| "error C3861: 'printf': identifier not found, even with argument-dependent
| lookup"
|
| If anyone has any clue as to what is going on, it would be greatly
| appreciated!!
|
| Thanks,
| Tim
|
|
|
|
|

Nov 15 '05 #2

HI Timothy,

On my machine, when I consume the dll, the printf works well and no error
generates.
Btw: In your second printf, you should use %d to show your data, if you use
%c, it will display it as Ascii.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| X-Tomcat-ID: 235590570
| References: <eN************ **@tk2msftngp13 .phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| Organization: Microsoft
| Date: Tue, 14 Oct 2003 09:37:27 GMT
| Subject: RE: Need help with importing unmanaged code
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| Message-ID: <Lq************ **@cpmsftngxa06 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| Lines: 132
| Path: cpmsftngxa06.ph x.gbl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1911 49
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
|
| Hi Timothy,
|
| Normally, there are 2 ways of interop with unmanaged dll, safe way and
| unsafe way.
| While the unsafe way is not recommanded, because its memory operations
are
| all not safe.
| Here is the safe way of consuming the unmanaged dll:
|
| [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| static extern void ReturnCharPoint er( byte []bytebuf , int buffersize,
| IntPtr charbuf, int charbufsize);
|
| int a = 100;
| int b = 75;
| byte[] bytebuf = new byte[a];
| IntPtr charbuf = Marshal.AllocHG lobal(b);
|
| for(int i =0; i <a; i++)
| {
| bytebuf[i]= Convert.ToByte( i);
| }
| ReturnCharPoint er(bytebuf,a,ch arbuf,b);
|
| for(int k=0;k<b;k++)
| {
| Console.WriteLi ne(Marshal.Read Byte(charbuf,k) );
| }
|
| In the last for loop, I use Marshal.Read* function to retrieve data from
| unmanaged memory. You can also create a char array in managed memory and
| use Marshal.Copy method to copy the unmanaged memory to managed memory,
| then it will be comfortable to use.
|
| If you still want to use unsafe way to interop the unmanaged dll, you
| should alloc the memory from unmanaged memory, but your code alloc from
the
| managed memory.
| You can do like this:
| [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| unsafe static extern void ReturnCharPoint er( byte *bytebuf , int
| buffersize, char * charbuf, int charbufsize);
|
| int a = 100;
| int b = 75;
|
| byte* bytebuf=(byte*) Marshal.AllocCo TaskMem(a);
| char* charbuf=(char*) Marshal.AllocCo TaskMem(b);
|
| for(int i =0; i <a; i++)
| {
| bytebuf[i]= Convert.ToByte( i);
| }
|
| ReturnCharPoint er( bytebuf,a, charbuf,b);
|
| After ReturnCharPoint er method, in the memory window, you can see that
the
| charbuf is what you want, but if you want to show it out, you should
| interop with some umanaged function such as printf, MessageBoxA to show
it
| out.
|
| Hope this helps,
|
| Best regards,
| Jeffrey Tan
| Microsoft Online Partner Support
| Get Secure! - www.microsoft.com/security
| This posting is provided "as is" with no warranties and confers no rights.
|
| --------------------
| | From: "Timothy Shih" <ts***@sensicas t.com>
| | Subject: Need help with importing unmanaged code
| | Date: Mon, 13 Oct 2003 11:08:15 -0400
| | Lines: 78
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | Message-ID: <eN************ **@tk2msftngp13 .phx.gbl>
| | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| | NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net 66.92.93.226
| | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
| | Xref: cpmsftngxa06.ph x.gbl
microsoft.publi c.dotnet.langua ges.csharp:1909 96
| | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| |
| | Hi, I am trying to figure out how to use unmanaged code using P/Invoke.
I
| | wrote a simple function which takes in 2 buffers (one a byte buffer,
one a
| | char buffer) and copies the contents of the byte buffer into the
character
| | pointer. The code looks like the following:
| |
| | #include <stdio.h>
| | #include <stdlib.h>
| | #include "stdafx.h"
| | BOOL APIENTRY DllMain( HANDLE hModule,
| | DWORD ul_reason_for_c all,
| | LPVOID lpReserved
| | )
| | {
| | return TRUE;
| | }
| |
| | extern "C" __declspec(dlle xport)
| | void ReturnCharPoint er(UINT8 *buffer, int buffersize, char *charbuf, int
| | charbufsize) {
| | int i =0;
| | for(i=0; i < charbufsize; i++) {
| | if(i>=buffersiz e)
| | return;
| | //printf("BYTE %d: %d\r\n",i,buffe r[i]);
| | charbuf[i] = buffer[i];
| | //printf("CHAR %d: %c\r\n",i,charb uf[i]);
| | }
| | }
| |
| | Now I try to import that function with the following line in a C#
project:
| |
| | [DllImport("DllS hell")]
| | unsafe static extern void ReturnCharPoint er(byte *buffer, int
buffersize,
| | char *charbuf, int charbufsize);
| |
| | I use this in the following function:
| |
| | unsafe private void ParseBuffer()
| | {
| | int a = 100;
| | int b = 75;
| | byte[] bytebuf = new byte[a];
| | char[] charbuf = new char[b];
| | for(int i =0; i <a; i++)
| | {
| | bytebuf[i]= Convert.ToByte( i);
| | }
| | fixed(byte *bbuf=bytebuf)
| | fixed(char *cbuf = charbuf)
| | ReturnCharPoint er(bbuf,a,cbuf, b);
| | for(int i =0; i < b; i++)
| | {
| | Console.Write(c harbuf[i] + ", ");
| | }
| |
| | }
| |
| | My problem is the following: I can import and run the code just fine.
| | However, when the function exits, the data in the character buffer is
not
| | what is expected! The byte buffer contains the numbers 0-99, the
character
| | buffer, whose size is 75 contains the following : [0] = 256, [1] = 770,
| [2]
| | = 1284, [3] = 1798.... etc. The rest of the array is incremented at the
| same
| | rate until index 37 which contains the value "74". Since 74 should be
the
| | last entry in the buffer, i suspect that the data is somehow being
| squashed
| | on the way into or out of the unmanaged C dll. Also I was unable to use
| | printf in the dll as it threw the following compiler error:
| | "error C3861: 'printf': identifier not found, even with
argument-dependent
| | lookup"
| |
| | If anyone has any clue as to what is going on, it would be greatly
| | appreciated!!
| |
| | Thanks,
| | Tim
| |
| |
| |
| |
| |
|
|

Nov 15 '05 #3

Hi Timothy,

You can also alloc your memory from managed memory, but you should marshal
it correctly.
Sample code like this:
[DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
static extern void
ReturnCharPoint er([In,Out,MarshalA s(UnmanagedType .LPArray,
SizeParamIndex= 1)] byte []bytebuf,int
buffersize,[In,Out,MarshalA s(UnmanagedType .LPArray, SizeParamIndex= 3)] char
[]charbuf,int charbufsize);

int a = 100;
int b = 75;
byte[] bytebuf = new byte[a];
char[] charbuf = new char[b];
for(int i =0; i <a; i++)
{
bytebuf[i]= Convert.ToByte( i);
}

ReturnCharPoint er( bytebuf,a, charbuf,b);
for(int k=0; k< b;k++)
{
Console.Write(C onvert.ToInt32( charbuf[k]) + ", ");
}

It works well on my machine, if you still have any question, please feel
free to let me know.

Btw: I think the strange display of your code is because in C# a char is
2-bytes, while in C/C++ a char is 1-byte long. You¡¯ve got an ASCII/UNICODE
mismatch when you are passing in the char* from C#.

Hope all these help

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| X-Tomcat-ID: 243569578
| References: <eN************ **@tk2msftngp13 .phx.gbl>
<Lq************ **@cpmsftngxa06 .phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| Organization: Microsoft
| Date: Tue, 14 Oct 2003 09:58:58 GMT
| Subject: RE: Need help with importing unmanaged code
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| Message-ID: <Da************ **@cpmsftngxa06 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| Lines: 169
| Path: cpmsftngxa06.ph x.gbl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1911 57
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
|
| HI Timothy,
|
| On my machine, when I consume the dll, the printf works well and no error
| generates.
| Btw: In your second printf, you should use %d to show your data, if you
use
| %c, it will display it as Ascii.
|
| Best regards,
| Jeffrey Tan
| Microsoft Online Partner Support
| Get Secure! - www.microsoft.com/security
| This posting is provided "as is" with no warranties and confers no rights.
|
| --------------------
| | X-Tomcat-ID: 235590570
| | References: <eN************ **@tk2msftngp13 .phx.gbl>
| | MIME-Version: 1.0
| | Content-Type: text/plain
| | Content-Transfer-Encoding: 7bit
| | From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| | Organization: Microsoft
| | Date: Tue, 14 Oct 2003 09:37:27 GMT
| | Subject: RE: Need help with importing unmanaged code
| | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| | Message-ID: <Lq************ **@cpmsftngxa06 .phx.gbl>
| | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| | Lines: 132
| | Path: cpmsftngxa06.ph x.gbl
| | Xref: cpmsftngxa06.ph x.gbl
microsoft.publi c.dotnet.langua ges.csharp:1911 49
| | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| |
| |
| | Hi Timothy,
| |
| | Normally, there are 2 ways of interop with unmanaged dll, safe way and
| | unsafe way.
| | While the unsafe way is not recommanded, because its memory operations
| are
| | all not safe.
| | Here is the safe way of consuming the unmanaged dll:
| |
| | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| | static extern void ReturnCharPoint er( byte []bytebuf , int buffersize,

| | IntPtr charbuf, int charbufsize);
| |
| | int a = 100;
| | int b = 75;
| | byte[] bytebuf = new byte[a];
| | IntPtr charbuf = Marshal.AllocHG lobal(b);
| |
| | for(int i =0; i <a; i++)
| | {
| | bytebuf[i]= Convert.ToByte( i);
| | }
| | ReturnCharPoint er(bytebuf,a,ch arbuf,b);
| |
| | for(int k=0;k<b;k++)
| | {
| | Console.WriteLi ne(Marshal.Read Byte(charbuf,k) );
| | }
| |
| | In the last for loop, I use Marshal.Read* function to retrieve data
from
| | unmanaged memory. You can also create a char array in managed memory
and
| | use Marshal.Copy method to copy the unmanaged memory to managed memory,
| | then it will be comfortable to use.
| |
| | If you still want to use unsafe way to interop the unmanaged dll, you
| | should alloc the memory from unmanaged memory, but your code alloc from
| the
| | managed memory.
| | You can do like this:
| | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| | unsafe static extern void ReturnCharPoint er( byte *bytebuf , int
| | buffersize, char * charbuf, int charbufsize);
| |
| | int a = 100;
| | int b = 75;
| |
| | byte* bytebuf=(byte*) Marshal.AllocCo TaskMem(a);
| | char* charbuf=(char*) Marshal.AllocCo TaskMem(b);
| |
| | for(int i =0; i <a; i++)
| | {
| | bytebuf[i]= Convert.ToByte( i);
| | }
| |
| | ReturnCharPoint er( bytebuf,a, charbuf,b);
| |
| | After ReturnCharPoint er method, in the memory window, you can see that
| the
| | charbuf is what you want, but if you want to show it out, you should
| | interop with some umanaged function such as printf, MessageBoxA to show
| it
| | out.
| |
| | Hope this helps,
| |
| | Best regards,
| | Jeffrey Tan
| | Microsoft Online Partner Support
| | Get Secure! - www.microsoft.com/security
| | This posting is provided "as is" with no warranties and confers no
rights.
| |
| | --------------------
| | | From: "Timothy Shih" <ts***@sensicas t.com>
| | | Subject: Need help with importing unmanaged code
| | | Date: Mon, 13 Oct 2003 11:08:15 -0400
| | | Lines: 78
| | | X-Priority: 3
| | | X-MSMail-Priority: Normal
| | | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | | Message-ID: <eN************ **@tk2msftngp13 .phx.gbl>
| | | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| | | NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net 66.92.93.226
| | | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
| | | Xref: cpmsftngxa06.ph x.gbl
| microsoft.publi c.dotnet.langua ges.csharp:1909 96
| | | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| | |
| | | Hi, I am trying to figure out how to use unmanaged code using
P/Invoke.
| I
| | | wrote a simple function which takes in 2 buffers (one a byte buffer,
| one a
| | | char buffer) and copies the contents of the byte buffer into the
| character
| | | pointer. The code looks like the following:
| | |
| | | #include <stdio.h>
| | | #include <stdlib.h>
| | | #include "stdafx.h"
| | | BOOL APIENTRY DllMain( HANDLE hModule,
| | | DWORD ul_reason_for_c all,
| | | LPVOID lpReserved
| | | )
| | | {
| | | return TRUE;
| | | }
| | |
| | | extern "C" __declspec(dlle xport)
| | | void ReturnCharPoint er(UINT8 *buffer, int buffersize, char *charbuf,
int
| | | charbufsize) {
| | | int i =0;
| | | for(i=0; i < charbufsize; i++) {
| | | if(i>=buffersiz e)
| | | return;
| | | //printf("BYTE %d: %d\r\n",i,buffe r[i]);
| | | charbuf[i] = buffer[i];
| | | //printf("CHAR %d: %c\r\n",i,charb uf[i]);
| | | }
| | | }
| | |
| | | Now I try to import that function with the following line in a C#
| project:
| | |
| | | [DllImport("DllS hell")]
| | | unsafe static extern void ReturnCharPoint er(byte *buffer, int
| buffersize,
| | | char *charbuf, int charbufsize);
| | |
| | | I use this in the following function:
| | |
| | | unsafe private void ParseBuffer()
| | | {
| | | int a = 100;
| | | int b = 75;
| | | byte[] bytebuf = new byte[a];
| | | char[] charbuf = new char[b];
| | | for(int i =0; i <a; i++)
| | | {
| | | bytebuf[i]= Convert.ToByte( i);
| | | }
| | | fixed(byte *bbuf=bytebuf)
| | | fixed(char *cbuf = charbuf)
| | | ReturnCharPoint er(bbuf,a,cbuf, b);
| | | for(int i =0; i < b; i++)
| | | {
| | | Console.Write(c harbuf[i] + ", ");
| | | }
| | |
| | | }
| | |
| | | My problem is the following: I can import and run the code just fine.
| | | However, when the function exits, the data in the character buffer is
| not
| | | what is expected! The byte buffer contains the numbers 0-99, the
| character
| | | buffer, whose size is 75 contains the following : [0] = 256, [1] =
770,
| | [2]
| | | = 1284, [3] = 1798.... etc. The rest of the array is incremented at
the
| | same
| | | rate until index 37 which contains the value "74". Since 74 should be
| the
| | | last entry in the buffer, i suspect that the data is somehow being
| | squashed
| | | on the way into or out of the unmanaged C dll. Also I was unable to
use
| | | printf in the dll as it threw the following compiler error:
| | | "error C3861: 'printf': identifier not found, even with
| argument-dependent
| | | lookup"
| | |
| | | If anyone has any clue as to what is going on, it would be greatly
| | | appreciated!!
| | |
| | | Thanks,
| | | Tim
| | |
| | |
| | |
| | |
| | |
| |
| |
|
|

Nov 15 '05 #4
Yep, that was the problem! Thanks a lot for your help. Also, I am still
unable to compile with printf, sprintf or fprintf, even though I have
#included stdio.h. Do you have any idea why I can't use these functions?

Thanks again!
Tim
""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
news:El******** ******@cpmsftng xa06.phx.gbl...

Hi Timothy,

You can also alloc your memory from managed memory, but you should marshal
it correctly.
Sample code like this:
[DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
static extern void
ReturnCharPoint er([In,Out,MarshalA s(UnmanagedType .LPArray,
SizeParamIndex= 1)] byte []bytebuf,int
buffersize,[In,Out,MarshalA s(UnmanagedType .LPArray, SizeParamIndex= 3)] char []charbuf,int charbufsize);

int a = 100;
int b = 75;
byte[] bytebuf = new byte[a];
char[] charbuf = new char[b];
for(int i =0; i <a; i++)
{
bytebuf[i]= Convert.ToByte( i);
}

ReturnCharPoint er( bytebuf,a, charbuf,b);
for(int k=0; k< b;k++)
{
Console.Write(C onvert.ToInt32( charbuf[k]) + ", ");
}

It works well on my machine, if you still have any question, please feel
free to let me know.

Btw: I think the strange display of your code is because in C# a char is
2-bytes, while in C/C++ a char is 1-byte long. You¡¯ve got an ASCII/UNICODE mismatch when you are passing in the char* from C#.

Hope all these help

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| X-Tomcat-ID: 243569578
| References: <eN************ **@tk2msftngp13 .phx.gbl>
<Lq************ **@cpmsftngxa06 .phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| Organization: Microsoft
| Date: Tue, 14 Oct 2003 09:58:58 GMT
| Subject: RE: Need help with importing unmanaged code
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| Message-ID: <Da************ **@cpmsftngxa06 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| Lines: 169
| Path: cpmsftngxa06.ph x.gbl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1911 57 | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
|
| HI Timothy,
|
| On my machine, when I consume the dll, the printf works well and no error | generates.
| Btw: In your second printf, you should use %d to show your data, if you
use
| %c, it will display it as Ascii.
|
| Best regards,
| Jeffrey Tan
| Microsoft Online Partner Support
| Get Secure! - www.microsoft.com/security
| This posting is provided "as is" with no warranties and confers no rights. |
| --------------------
| | X-Tomcat-ID: 235590570
| | References: <eN************ **@tk2msftngp13 .phx.gbl>
| | MIME-Version: 1.0
| | Content-Type: text/plain
| | Content-Transfer-Encoding: 7bit
| | From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| | Organization: Microsoft
| | Date: Tue, 14 Oct 2003 09:37:27 GMT
| | Subject: RE: Need help with importing unmanaged code
| | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| | Message-ID: <Lq************ **@cpmsftngxa06 .phx.gbl>
| | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| | Lines: 132
| | Path: cpmsftngxa06.ph x.gbl
| | Xref: cpmsftngxa06.ph x.gbl
microsoft.publi c.dotnet.langua ges.csharp:1911 49
| | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| |
| |
| | Hi Timothy,
| |
| | Normally, there are 2 ways of interop with unmanaged dll, safe way and
| | unsafe way.
| | While the unsafe way is not recommanded, because its memory operations
| are
| | all not safe.
| | Here is the safe way of consuming the unmanaged dll:
| |
| | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| | static extern void ReturnCharPoint er( byte []bytebuf , int buffersize,

| | IntPtr charbuf, int charbufsize);
| |
| | int a = 100;
| | int b = 75;
| | byte[] bytebuf = new byte[a];
| | IntPtr charbuf = Marshal.AllocHG lobal(b);
| |
| | for(int i =0; i <a; i++)
| | {
| | bytebuf[i]= Convert.ToByte( i);
| | }
| | ReturnCharPoint er(bytebuf,a,ch arbuf,b);
| |
| | for(int k=0;k<b;k++)
| | {
| | Console.WriteLi ne(Marshal.Read Byte(charbuf,k) );
| | }
| |
| | In the last for loop, I use Marshal.Read* function to retrieve data
from
| | unmanaged memory. You can also create a char array in managed memory
and
| | use Marshal.Copy method to copy the unmanaged memory to managed memory, | | then it will be comfortable to use.
| |
| | If you still want to use unsafe way to interop the unmanaged dll, you
| | should alloc the memory from unmanaged memory, but your code alloc from | the
| | managed memory.
| | You can do like this:
| | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| | unsafe static extern void ReturnCharPoint er( byte *bytebuf , int
| | buffersize, char * charbuf, int charbufsize);
| |
| | int a = 100;
| | int b = 75;
| |
| | byte* bytebuf=(byte*) Marshal.AllocCo TaskMem(a);
| | char* charbuf=(char*) Marshal.AllocCo TaskMem(b);
| |
| | for(int i =0; i <a; i++)
| | {
| | bytebuf[i]= Convert.ToByte( i);
| | }
| |
| | ReturnCharPoint er( bytebuf,a, charbuf,b);
| |
| | After ReturnCharPoint er method, in the memory window, you can see that
| the
| | charbuf is what you want, but if you want to show it out, you should
| | interop with some umanaged function such as printf, MessageBoxA to show | it
| | out.
| |
| | Hope this helps,
| |
| | Best regards,
| | Jeffrey Tan
| | Microsoft Online Partner Support
| | Get Secure! - www.microsoft.com/security
| | This posting is provided "as is" with no warranties and confers no
rights.
| |
| | --------------------
| | | From: "Timothy Shih" <ts***@sensicas t.com>
| | | Subject: Need help with importing unmanaged code
| | | Date: Mon, 13 Oct 2003 11:08:15 -0400
| | | Lines: 78
| | | X-Priority: 3
| | | X-MSMail-Priority: Normal
| | | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | | Message-ID: <eN************ **@tk2msftngp13 .phx.gbl>
| | | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| | | NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net 66.92.93.226 | | | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
| | | Xref: cpmsftngxa06.ph x.gbl
| microsoft.publi c.dotnet.langua ges.csharp:1909 96
| | | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| | |
| | | Hi, I am trying to figure out how to use unmanaged code using
P/Invoke.
| I
| | | wrote a simple function which takes in 2 buffers (one a byte buffer,
| one a
| | | char buffer) and copies the contents of the byte buffer into the
| character
| | | pointer. The code looks like the following:
| | |
| | | #include <stdio.h>
| | | #include <stdlib.h>
| | | #include "stdafx.h"
| | | BOOL APIENTRY DllMain( HANDLE hModule,
| | | DWORD ul_reason_for_c all,
| | | LPVOID lpReserved
| | | )
| | | {
| | | return TRUE;
| | | }
| | |
| | | extern "C" __declspec(dlle xport)
| | | void ReturnCharPoint er(UINT8 *buffer, int buffersize, char *charbuf,
int
| | | charbufsize) {
| | | int i =0;
| | | for(i=0; i < charbufsize; i++) {
| | | if(i>=buffersiz e)
| | | return;
| | | //printf("BYTE %d: %d\r\n",i,buffe r[i]);
| | | charbuf[i] = buffer[i];
| | | //printf("CHAR %d: %c\r\n",i,charb uf[i]);
| | | }
| | | }
| | |
| | | Now I try to import that function with the following line in a C#
| project:
| | |
| | | [DllImport("DllS hell")]
| | | unsafe static extern void ReturnCharPoint er(byte *buffer, int
| buffersize,
| | | char *charbuf, int charbufsize);
| | |
| | | I use this in the following function:
| | |
| | | unsafe private void ParseBuffer()
| | | {
| | | int a = 100;
| | | int b = 75;
| | | byte[] bytebuf = new byte[a];
| | | char[] charbuf = new char[b];
| | | for(int i =0; i <a; i++)
| | | {
| | | bytebuf[i]= Convert.ToByte( i);
| | | }
| | | fixed(byte *bbuf=bytebuf)
| | | fixed(char *cbuf = charbuf)
| | | ReturnCharPoint er(bbuf,a,cbuf, b);
| | | for(int i =0; i < b; i++)
| | | {
| | | Console.Write(c harbuf[i] + ", ");
| | | }
| | |
| | | }
| | |
| | | My problem is the following: I can import and run the code just fine. | | | However, when the function exits, the data in the character buffer is | not
| | | what is expected! The byte buffer contains the numbers 0-99, the
| character
| | | buffer, whose size is 75 contains the following : [0] = 256, [1] =
770,
| | [2]
| | | = 1284, [3] = 1798.... etc. The rest of the array is incremented at
the
| | same
| | | rate until index 37 which contains the value "74". Since 74 should be | the
| | | last entry in the buffer, i suspect that the data is somehow being
| | squashed
| | | on the way into or out of the unmanaged C dll. Also I was unable to
use
| | | printf in the dll as it threw the following compiler error:
| | | "error C3861: 'printf': identifier not found, even with
| argument-dependent
| | | lookup"
| | |
| | | If anyone has any clue as to what is going on, it would be greatly
| | | appreciated!!
| | |
| | | Thanks,
| | | Tim
| | |
| | |
| | |
| | |
| | |
| |
| |
|
|

Nov 15 '05 #5

Hi Timothy,

I am glad my research does make sence to you.
For your dll problem, I think you can try to create a new dll project as
"Win32 Dynammic-Link Library" type.
And paste your code, but replace your {#include "stdafx.h"} with {#include
<windows.h>}, I did this and it works well on my machine.

If it still do not work, you can try to create a win32 executive
application to find if this printf generates error.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Timothy Shih" <ts***@sensicas t.com>
| References: <eN************ **@tk2msftngp13 .phx.gbl>
<Lq************ **@cpmsftngxa06 .phx.gbl>
<Da************ **@cpmsftngxa06 .phx.gbl>
<El************ **@cpmsftngxa06 .phx.gbl>
| Subject: Re: Need help with importing unmanaged code
| Date: Fri, 17 Oct 2003 14:34:14 -0400
| Lines: 314
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <Od************ **@TK2MSFTNGP09 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net 66.92.93.226
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP09.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1921 65
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Yep, that was the problem! Thanks a lot for your help. Also, I am still
| unable to compile with printf, sprintf or fprintf, even though I have
| #included stdio.h. Do you have any idea why I can't use these functions?
|
| Thanks again!
| Tim
|
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
| news:El******** ******@cpmsftng xa06.phx.gbl...
| >
| > Hi Timothy,
| >
| > You can also alloc your memory from managed memory, but you should
marshal
| > it correctly.
| > Sample code like this:
| > [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| > static extern void
| > ReturnCharPoint er([In,Out,MarshalA s(UnmanagedType .LPArray,
| > SizeParamIndex= 1)] byte []bytebuf,int
| > buffersize,[In,Out,MarshalA s(UnmanagedType .LPArray, SizeParamIndex= 3)]
| char
| > []charbuf,int charbufsize);
| >
| > int a = 100;
| > int b = 75;
| > byte[] bytebuf = new byte[a];
| > char[] charbuf = new char[b];
| > for(int i =0; i <a; i++)
| > {
| > bytebuf[i]= Convert.ToByte( i);
| > }
| >
| > ReturnCharPoint er( bytebuf,a, charbuf,b);
| > for(int k=0; k< b;k++)
| > {
| > Console.Write(C onvert.ToInt32( charbuf[k]) + ", ");
| > }
| >
| > It works well on my machine, if you still have any question, please feel
| > free to let me know.
| >
| > Btw: I think the strange display of your code is because in C# a char is
| > 2-bytes, while in C/C++ a char is 1-byte long. You¡¯ve got an
| ASCII/UNICODE
| > mismatch when you are passing in the char* from C#.
| >
| > Hope all these help
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | X-Tomcat-ID: 243569578
| > | References: <eN************ **@tk2msftngp13 .phx.gbl>
| > <Lq************ **@cpmsftngxa06 .phx.gbl>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain
| > | Content-Transfer-Encoding: 7bit
| > | From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| > | Organization: Microsoft
| > | Date: Tue, 14 Oct 2003 09:58:58 GMT
| > | Subject: RE: Need help with importing unmanaged code
| > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| > | Message-ID: <Da************ **@cpmsftngxa06 .phx.gbl>
| > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | Lines: 169
| > | Path: cpmsftngxa06.ph x.gbl
| > | Xref: cpmsftngxa06.ph x.gbl
| microsoft.publi c.dotnet.langua ges.csharp:1911 57
| > | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| > |
| > |
| > | HI Timothy,
| > |
| > | On my machine, when I consume the dll, the printf works well and no
| error
| > | generates.
| > | Btw: In your second printf, you should use %d to show your data, if
you
| > use
| > | %c, it will display it as Ascii.
| > |
| > | Best regards,
| > | Jeffrey Tan
| > | Microsoft Online Partner Support
| > | Get Secure! - www.microsoft.com/security
| > | This posting is provided "as is" with no warranties and confers no
| rights.
| > |
| > | --------------------
| > | | X-Tomcat-ID: 235590570
| > | | References: <eN************ **@tk2msftngp13 .phx.gbl>
| > | | MIME-Version: 1.0
| > | | Content-Type: text/plain
| > | | Content-Transfer-Encoding: 7bit
| > | | From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| > | | Organization: Microsoft
| > | | Date: Tue, 14 Oct 2003 09:37:27 GMT
| > | | Subject: RE: Need help with importing unmanaged code
| > | | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| > | | Message-ID: <Lq************ **@cpmsftngxa06 .phx.gbl>
| > | | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | | Lines: 132
| > | | Path: cpmsftngxa06.ph x.gbl
| > | | Xref: cpmsftngxa06.ph x.gbl
| > microsoft.publi c.dotnet.langua ges.csharp:1911 49
| > | | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| > | |
| > | |
| > | | Hi Timothy,
| > | |
| > | | Normally, there are 2 ways of interop with unmanaged dll, safe way
and
| > | | unsafe way.
| > | | While the unsafe way is not recommanded, because its memory
operations
| > | are
| > | | all not safe.
| > | | Here is the safe way of consuming the unmanaged dll:
| > | |
| > | | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| > | | static extern void ReturnCharPoint er( byte []bytebuf , int
buffersize,
| >
| > | | IntPtr charbuf, int charbufsize);
| > | |
| > | | int a = 100;
| > | | int b = 75;
| > | | byte[] bytebuf = new byte[a];
| > | | IntPtr charbuf = Marshal.AllocHG lobal(b);
| > | |
| > | | for(int i =0; i <a; i++)
| > | | {
| > | | bytebuf[i]= Convert.ToByte( i);
| > | | }
| > | | ReturnCharPoint er(bytebuf,a,ch arbuf,b);
| > | |
| > | | for(int k=0;k<b;k++)
| > | | {
| > | | Console.WriteLi ne(Marshal.Read Byte(charbuf,k) );
| > | | }
| > | |
| > | | In the last for loop, I use Marshal.Read* function to retrieve data
| > from
| > | | unmanaged memory. You can also create a char array in managed memory
| > and
| > | | use Marshal.Copy method to copy the unmanaged memory to managed
| memory,
| > | | then it will be comfortable to use.
| > | |
| > | | If you still want to use unsafe way to interop the unmanaged dll,
you
| > | | should alloc the memory from unmanaged memory, but your code alloc
| from
| > | the
| > | | managed memory.
| > | | You can do like this:
| > | | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| > | | unsafe static extern void ReturnCharPoint er( byte *bytebuf , int
| > | | buffersize, char * charbuf, int charbufsize);
| > | |
| > | | int a = 100;
| > | | int b = 75;
| > | |
| > | | byte* bytebuf=(byte*) Marshal.AllocCo TaskMem(a);
| > | | char* charbuf=(char*) Marshal.AllocCo TaskMem(b);
| > | |
| > | | for(int i =0; i <a; i++)
| > | | {
| > | | bytebuf[i]= Convert.ToByte( i);
| > | | }
| > | |
| > | | ReturnCharPoint er( bytebuf,a, charbuf,b);
| > | |
| > | | After ReturnCharPoint er method, in the memory window, you can see
that
| > | the
| > | | charbuf is what you want, but if you want to show it out, you should
| > | | interop with some umanaged function such as printf, MessageBoxA to
| show
| > | it
| > | | out.
| > | |
| > | | Hope this helps,
| > | |
| > | | Best regards,
| > | | Jeffrey Tan
| > | | Microsoft Online Partner Support
| > | | Get Secure! - www.microsoft.com/security
| > | | This posting is provided "as is" with no warranties and confers no
| > rights.
| > | |
| > | | --------------------
| > | | | From: "Timothy Shih" <ts***@sensicas t.com>
| > | | | Subject: Need help with importing unmanaged code
| > | | | Date: Mon, 13 Oct 2003 11:08:15 -0400
| > | | | Lines: 78
| > | | | X-Priority: 3
| > | | | X-MSMail-Priority: Normal
| > | | | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | | | Message-ID: <eN************ **@tk2msftngp13 .phx.gbl>
| > | | | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | | | NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net
| 66.92.93.226
| > | | | Path:
cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
| > | | | Xref: cpmsftngxa06.ph x.gbl
| > | microsoft.publi c.dotnet.langua ges.csharp:1909 96
| > | | | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| > | | |
| > | | | Hi, I am trying to figure out how to use unmanaged code using
| > P/Invoke.
| > | I
| > | | | wrote a simple function which takes in 2 buffers (one a byte
buffer,
| > | one a
| > | | | char buffer) and copies the contents of the byte buffer into the
| > | character
| > | | | pointer. The code looks like the following:
| > | | |
| > | | | #include <stdio.h>
| > | | | #include <stdlib.h>
| > | | | #include "stdafx.h"
| > | | | BOOL APIENTRY DllMain( HANDLE hModule,
| > | | | DWORD ul_reason_for_c all,
| > | | | LPVOID lpReserved
| > | | | )
| > | | | {
| > | | | return TRUE;
| > | | | }
| > | | |
| > | | | extern "C" __declspec(dlle xport)
| > | | | void ReturnCharPoint er(UINT8 *buffer, int buffersize, char
*charbuf,
| > int
| > | | | charbufsize) {
| > | | | int i =0;
| > | | | for(i=0; i < charbufsize; i++) {
| > | | | if(i>=buffersiz e)
| > | | | return;
| > | | | //printf("BYTE %d: %d\r\n",i,buffe r[i]);
| > | | | charbuf[i] = buffer[i];
| > | | | //printf("CHAR %d: %c\r\n",i,charb uf[i]);
| > | | | }
| > | | | }
| > | | |
| > | | | Now I try to import that function with the following line in a C#
| > | project:
| > | | |
| > | | | [DllImport("DllS hell")]
| > | | | unsafe static extern void ReturnCharPoint er(byte *buffer, int
| > | buffersize,
| > | | | char *charbuf, int charbufsize);
| > | | |
| > | | | I use this in the following function:
| > | | |
| > | | | unsafe private void ParseBuffer()
| > | | | {
| > | | | int a = 100;
| > | | | int b = 75;
| > | | | byte[] bytebuf = new byte[a];
| > | | | char[] charbuf = new char[b];
| > | | | for(int i =0; i <a; i++)
| > | | | {
| > | | | bytebuf[i]= Convert.ToByte( i);
| > | | | }
| > | | | fixed(byte *bbuf=bytebuf)
| > | | | fixed(char *cbuf = charbuf)
| > | | | ReturnCharPoint er(bbuf,a,cbuf, b);
| > | | | for(int i =0; i < b; i++)
| > | | | {
| > | | | Console.Write(c harbuf[i] + ", ");
| > | | | }
| > | | |
| > | | | }
| > | | |
| > | | | My problem is the following: I can import and run the code just
| fine.
| > | | | However, when the function exits, the data in the character buffer
| is
| > | not
| > | | | what is expected! The byte buffer contains the numbers 0-99, the
| > | character
| > | | | buffer, whose size is 75 contains the following : [0] = 256, [1] =
| > 770,
| > | | [2]
| > | | | = 1284, [3] = 1798.... etc. The rest of the array is incremented
at
| > the
| > | | same
| > | | | rate until index 37 which contains the value "74". Since 74 should
| be
| > | the
| > | | | last entry in the buffer, i suspect that the data is somehow being
| > | | squashed
| > | | | on the way into or out of the unmanaged C dll. Also I was unable
to
| > use
| > | | | printf in the dll as it threw the following compiler error:
| > | | | "error C3861: 'printf': identifier not found, even with
| > | argument-dependent
| > | | | lookup"
| > | | |
| > | | | If anyone has any clue as to what is going on, it would be greatly
| > | | | appreciated!!
| > | | |
| > | | | Thanks,
| > | | | Tim
| > | | |
| > | | |
| > | | |
| > | | |
| > | | |
| > | |
| > | |
| > |
| > |
| >
|
|
|

Nov 15 '05 #6

Hi Timothy,

Has your problem been resolved?
If it still does not work, please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Timothy Shih" <ts***@sensicas t.com>
| References: <eN************ **@tk2msftngp13 .phx.gbl>
<Lq************ **@cpmsftngxa06 .phx.gbl>
<Da************ **@cpmsftngxa06 .phx.gbl>
<El************ **@cpmsftngxa06 .phx.gbl>
| Subject: Re: Need help with importing unmanaged code
| Date: Fri, 17 Oct 2003 14:34:14 -0400
| Lines: 314
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <Od************ **@TK2MSFTNGP09 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net 66.92.93.226
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP09.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1921 65
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Yep, that was the problem! Thanks a lot for your help. Also, I am still
| unable to compile with printf, sprintf or fprintf, even though I have
| #included stdio.h. Do you have any idea why I can't use these functions?
|
| Thanks again!
| Tim
|
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
| news:El******** ******@cpmsftng xa06.phx.gbl...
| >
| > Hi Timothy,
| >
| > You can also alloc your memory from managed memory, but you should
marshal
| > it correctly.
| > Sample code like this:
| > [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| > static extern void
| > ReturnCharPoint er([In,Out,MarshalA s(UnmanagedType .LPArray,
| > SizeParamIndex= 1)] byte []bytebuf,int
| > buffersize,[In,Out,MarshalA s(UnmanagedType .LPArray, SizeParamIndex= 3)]
| char
| > []charbuf,int charbufsize);
| >
| > int a = 100;
| > int b = 75;
| > byte[] bytebuf = new byte[a];
| > char[] charbuf = new char[b];
| > for(int i =0; i <a; i++)
| > {
| > bytebuf[i]= Convert.ToByte( i);
| > }
| >
| > ReturnCharPoint er( bytebuf,a, charbuf,b);
| > for(int k=0; k< b;k++)
| > {
| > Console.Write(C onvert.ToInt32( charbuf[k]) + ", ");
| > }
| >
| > It works well on my machine, if you still have any question, please feel
| > free to let me know.
| >
| > Btw: I think the strange display of your code is because in C# a char is
| > 2-bytes, while in C/C++ a char is 1-byte long. You¡¯ve got an
| ASCII/UNICODE
| > mismatch when you are passing in the char* from C#.
| >
| > Hope all these help
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | X-Tomcat-ID: 243569578
| > | References: <eN************ **@tk2msftngp13 .phx.gbl>
| > <Lq************ **@cpmsftngxa06 .phx.gbl>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain
| > | Content-Transfer-Encoding: 7bit
| > | From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| > | Organization: Microsoft
| > | Date: Tue, 14 Oct 2003 09:58:58 GMT
| > | Subject: RE: Need help with importing unmanaged code
| > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| > | Message-ID: <Da************ **@cpmsftngxa06 .phx.gbl>
| > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | Lines: 169
| > | Path: cpmsftngxa06.ph x.gbl
| > | Xref: cpmsftngxa06.ph x.gbl
| microsoft.publi c.dotnet.langua ges.csharp:1911 57
| > | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| > |
| > |
| > | HI Timothy,
| > |
| > | On my machine, when I consume the dll, the printf works well and no
| error
| > | generates.
| > | Btw: In your second printf, you should use %d to show your data, if
you
| > use
| > | %c, it will display it as Ascii.
| > |
| > | Best regards,
| > | Jeffrey Tan
| > | Microsoft Online Partner Support
| > | Get Secure! - www.microsoft.com/security
| > | This posting is provided "as is" with no warranties and confers no
| rights.
| > |
| > | --------------------
| > | | X-Tomcat-ID: 235590570
| > | | References: <eN************ **@tk2msftngp13 .phx.gbl>
| > | | MIME-Version: 1.0
| > | | Content-Type: text/plain
| > | | Content-Transfer-Encoding: 7bit
| > | | From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| > | | Organization: Microsoft
| > | | Date: Tue, 14 Oct 2003 09:37:27 GMT
| > | | Subject: RE: Need help with importing unmanaged code
| > | | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| > | | Message-ID: <Lq************ **@cpmsftngxa06 .phx.gbl>
| > | | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | | Lines: 132
| > | | Path: cpmsftngxa06.ph x.gbl
| > | | Xref: cpmsftngxa06.ph x.gbl
| > microsoft.publi c.dotnet.langua ges.csharp:1911 49
| > | | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| > | |
| > | |
| > | | Hi Timothy,
| > | |
| > | | Normally, there are 2 ways of interop with unmanaged dll, safe way
and
| > | | unsafe way.
| > | | While the unsafe way is not recommanded, because its memory
operations
| > | are
| > | | all not safe.
| > | | Here is the safe way of consuming the unmanaged dll:
| > | |
| > | | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| > | | static extern void ReturnCharPoint er( byte []bytebuf , int
buffersize,
| >
| > | | IntPtr charbuf, int charbufsize);
| > | |
| > | | int a = 100;
| > | | int b = 75;
| > | | byte[] bytebuf = new byte[a];
| > | | IntPtr charbuf = Marshal.AllocHG lobal(b);
| > | |
| > | | for(int i =0; i <a; i++)
| > | | {
| > | | bytebuf[i]= Convert.ToByte( i);
| > | | }
| > | | ReturnCharPoint er(bytebuf,a,ch arbuf,b);
| > | |
| > | | for(int k=0;k<b;k++)
| > | | {
| > | | Console.WriteLi ne(Marshal.Read Byte(charbuf,k) );
| > | | }
| > | |
| > | | In the last for loop, I use Marshal.Read* function to retrieve data
| > from
| > | | unmanaged memory. You can also create a char array in managed memory
| > and
| > | | use Marshal.Copy method to copy the unmanaged memory to managed
| memory,
| > | | then it will be comfortable to use.
| > | |
| > | | If you still want to use unsafe way to interop the unmanaged dll,
you
| > | | should alloc the memory from unmanaged memory, but your code alloc
| from
| > | the
| > | | managed memory.
| > | | You can do like this:
| > | | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| > | | unsafe static extern void ReturnCharPoint er( byte *bytebuf , int
| > | | buffersize, char * charbuf, int charbufsize);
| > | |
| > | | int a = 100;
| > | | int b = 75;
| > | |
| > | | byte* bytebuf=(byte*) Marshal.AllocCo TaskMem(a);
| > | | char* charbuf=(char*) Marshal.AllocCo TaskMem(b);
| > | |
| > | | for(int i =0; i <a; i++)
| > | | {
| > | | bytebuf[i]= Convert.ToByte( i);
| > | | }
| > | |
| > | | ReturnCharPoint er( bytebuf,a, charbuf,b);
| > | |
| > | | After ReturnCharPoint er method, in the memory window, you can see
that
| > | the
| > | | charbuf is what you want, but if you want to show it out, you should
| > | | interop with some umanaged function such as printf, MessageBoxA to
| show
| > | it
| > | | out.
| > | |
| > | | Hope this helps,
| > | |
| > | | Best regards,
| > | | Jeffrey Tan
| > | | Microsoft Online Partner Support
| > | | Get Secure! - www.microsoft.com/security
| > | | This posting is provided "as is" with no warranties and confers no
| > rights.
| > | |
| > | | --------------------
| > | | | From: "Timothy Shih" <ts***@sensicas t.com>
| > | | | Subject: Need help with importing unmanaged code
| > | | | Date: Mon, 13 Oct 2003 11:08:15 -0400
| > | | | Lines: 78
| > | | | X-Priority: 3
| > | | | X-MSMail-Priority: Normal
| > | | | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | | | Message-ID: <eN************ **@tk2msftngp13 .phx.gbl>
| > | | | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | | | NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net
| 66.92.93.226
| > | | | Path:
cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
| > | | | Xref: cpmsftngxa06.ph x.gbl
| > | microsoft.publi c.dotnet.langua ges.csharp:1909 96
| > | | | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| > | | |
| > | | | Hi, I am trying to figure out how to use unmanaged code using
| > P/Invoke.
| > | I
| > | | | wrote a simple function which takes in 2 buffers (one a byte
buffer,
| > | one a
| > | | | char buffer) and copies the contents of the byte buffer into the
| > | character
| > | | | pointer. The code looks like the following:
| > | | |
| > | | | #include <stdio.h>
| > | | | #include <stdlib.h>
| > | | | #include "stdafx.h"
| > | | | BOOL APIENTRY DllMain( HANDLE hModule,
| > | | | DWORD ul_reason_for_c all,
| > | | | LPVOID lpReserved
| > | | | )
| > | | | {
| > | | | return TRUE;
| > | | | }
| > | | |
| > | | | extern "C" __declspec(dlle xport)
| > | | | void ReturnCharPoint er(UINT8 *buffer, int buffersize, char
*charbuf,
| > int
| > | | | charbufsize) {
| > | | | int i =0;
| > | | | for(i=0; i < charbufsize; i++) {
| > | | | if(i>=buffersiz e)
| > | | | return;
| > | | | //printf("BYTE %d: %d\r\n",i,buffe r[i]);
| > | | | charbuf[i] = buffer[i];
| > | | | //printf("CHAR %d: %c\r\n",i,charb uf[i]);
| > | | | }
| > | | | }
| > | | |
| > | | | Now I try to import that function with the following line in a C#
| > | project:
| > | | |
| > | | | [DllImport("DllS hell")]
| > | | | unsafe static extern void ReturnCharPoint er(byte *buffer, int
| > | buffersize,
| > | | | char *charbuf, int charbufsize);
| > | | |
| > | | | I use this in the following function:
| > | | |
| > | | | unsafe private void ParseBuffer()
| > | | | {
| > | | | int a = 100;
| > | | | int b = 75;
| > | | | byte[] bytebuf = new byte[a];
| > | | | char[] charbuf = new char[b];
| > | | | for(int i =0; i <a; i++)
| > | | | {
| > | | | bytebuf[i]= Convert.ToByte( i);
| > | | | }
| > | | | fixed(byte *bbuf=bytebuf)
| > | | | fixed(char *cbuf = charbuf)
| > | | | ReturnCharPoint er(bbuf,a,cbuf, b);
| > | | | for(int i =0; i < b; i++)
| > | | | {
| > | | | Console.Write(c harbuf[i] + ", ");
| > | | | }
| > | | |
| > | | | }
| > | | |
| > | | | My problem is the following: I can import and run the code just
| fine.
| > | | | However, when the function exits, the data in the character buffer
| is
| > | not
| > | | | what is expected! The byte buffer contains the numbers 0-99, the
| > | character
| > | | | buffer, whose size is 75 contains the following : [0] = 256, [1] =
| > 770,
| > | | [2]
| > | | | = 1284, [3] = 1798.... etc. The rest of the array is incremented
at
| > the
| > | | same
| > | | | rate until index 37 which contains the value "74". Since 74 should
| be
| > | the
| > | | | last entry in the buffer, i suspect that the data is somehow being
| > | | squashed
| > | | | on the way into or out of the unmanaged C dll. Also I was unable
to
| > use
| > | | | printf in the dll as it threw the following compiler error:
| > | | | "error C3861: 'printf': identifier not found, even with
| > | argument-dependent
| > | | | lookup"
| > | | |
| > | | | If anyone has any clue as to what is going on, it would be greatly
| > | | | appreciated!!
| > | | |
| > | | | Thanks,
| > | | | Tim
| > | | |
| > | | |
| > | | |
| > | | |
| > | | |
| > | |
| > | |
| > |
| > |
| >
|
|
|

Nov 15 '05 #7
Yes, it turned out that the #include for stdafx.h needed to be first before
any other includes, for some reason. Thanks for your help!

Tim
""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
news:7J******** ******@cpmsftng xa06.phx.gbl...

Hi Timothy,

Has your problem been resolved?
If it still does not work, please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Timothy Shih" <ts***@sensicas t.com>
| References: <eN************ **@tk2msftngp13 .phx.gbl>
<Lq************ **@cpmsftngxa06 .phx.gbl>
<Da************ **@cpmsftngxa06 .phx.gbl>
<El************ **@cpmsftngxa06 .phx.gbl>
| Subject: Re: Need help with importing unmanaged code
| Date: Fri, 17 Oct 2003 14:34:14 -0400
| Lines: 314
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <Od************ **@TK2MSFTNGP09 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net 66.92.93.226
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP09.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1921 65 | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Yep, that was the problem! Thanks a lot for your help. Also, I am still
| unable to compile with printf, sprintf or fprintf, even though I have
| #included stdio.h. Do you have any idea why I can't use these functions?
|
| Thanks again!
| Tim
|
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
| news:El******** ******@cpmsftng xa06.phx.gbl...
| >
| > Hi Timothy,
| >
| > You can also alloc your memory from managed memory, but you should
marshal
| > it correctly.
| > Sample code like this:
| > [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| > static extern void
| > ReturnCharPoint er([In,Out,MarshalA s(UnmanagedType .LPArray,
| > SizeParamIndex= 1)] byte []bytebuf,int
| > buffersize,[In,Out,MarshalA s(UnmanagedType .LPArray, SizeParamIndex= 3)]
| char
| > []charbuf,int charbufsize);
| >
| > int a = 100;
| > int b = 75;
| > byte[] bytebuf = new byte[a];
| > char[] charbuf = new char[b];
| > for(int i =0; i <a; i++)
| > {
| > bytebuf[i]= Convert.ToByte( i);
| > }
| >
| > ReturnCharPoint er( bytebuf,a, charbuf,b);
| > for(int k=0; k< b;k++)
| > {
| > Console.Write(C onvert.ToInt32( charbuf[k]) + ", ");
| > }
| >
| > It works well on my machine, if you still have any question, please feel | > free to let me know.
| >
| > Btw: I think the strange display of your code is because in C# a char is | > 2-bytes, while in C/C++ a char is 1-byte long. You¡¯ve got an
| ASCII/UNICODE
| > mismatch when you are passing in the char* from C#.
| >
| > Hope all these help
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | X-Tomcat-ID: 243569578
| > | References: <eN************ **@tk2msftngp13 .phx.gbl>
| > <Lq************ **@cpmsftngxa06 .phx.gbl>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain
| > | Content-Transfer-Encoding: 7bit
| > | From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| > | Organization: Microsoft
| > | Date: Tue, 14 Oct 2003 09:58:58 GMT
| > | Subject: RE: Need help with importing unmanaged code
| > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| > | Message-ID: <Da************ **@cpmsftngxa06 .phx.gbl>
| > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | Lines: 169
| > | Path: cpmsftngxa06.ph x.gbl
| > | Xref: cpmsftngxa06.ph x.gbl
| microsoft.publi c.dotnet.langua ges.csharp:1911 57
| > | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| > |
| > |
| > | HI Timothy,
| > |
| > | On my machine, when I consume the dll, the printf works well and no
| error
| > | generates.
| > | Btw: In your second printf, you should use %d to show your data, if
you
| > use
| > | %c, it will display it as Ascii.
| > |
| > | Best regards,
| > | Jeffrey Tan
| > | Microsoft Online Partner Support
| > | Get Secure! - www.microsoft.com/security
| > | This posting is provided "as is" with no warranties and confers no
| rights.
| > |
| > | --------------------
| > | | X-Tomcat-ID: 235590570
| > | | References: <eN************ **@tk2msftngp13 .phx.gbl>
| > | | MIME-Version: 1.0
| > | | Content-Type: text/plain
| > | | Content-Transfer-Encoding: 7bit
| > | | From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| > | | Organization: Microsoft
| > | | Date: Tue, 14 Oct 2003 09:37:27 GMT
| > | | Subject: RE: Need help with importing unmanaged code
| > | | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| > | | Message-ID: <Lq************ **@cpmsftngxa06 .phx.gbl>
| > | | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | | Lines: 132
| > | | Path: cpmsftngxa06.ph x.gbl
| > | | Xref: cpmsftngxa06.ph x.gbl
| > microsoft.publi c.dotnet.langua ges.csharp:1911 49
| > | | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| > | |
| > | |
| > | | Hi Timothy,
| > | |
| > | | Normally, there are 2 ways of interop with unmanaged dll, safe way
and
| > | | unsafe way.
| > | | While the unsafe way is not recommanded, because its memory
operations
| > | are
| > | | all not safe.
| > | | Here is the safe way of consuming the unmanaged dll:
| > | |
| > | | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| > | | static extern void ReturnCharPoint er( byte []bytebuf , int
buffersize,
| >
| > | | IntPtr charbuf, int charbufsize);
| > | |
| > | | int a = 100;
| > | | int b = 75;
| > | | byte[] bytebuf = new byte[a];
| > | | IntPtr charbuf = Marshal.AllocHG lobal(b);
| > | |
| > | | for(int i =0; i <a; i++)
| > | | {
| > | | bytebuf[i]= Convert.ToByte( i);
| > | | }
| > | | ReturnCharPoint er(bytebuf,a,ch arbuf,b);
| > | |
| > | | for(int k=0;k<b;k++)
| > | | {
| > | | Console.WriteLi ne(Marshal.Read Byte(charbuf,k) );
| > | | }
| > | |
| > | | In the last for loop, I use Marshal.Read* function to retrieve data | > from
| > | | unmanaged memory. You can also create a char array in managed memory | > and
| > | | use Marshal.Copy method to copy the unmanaged memory to managed
| memory,
| > | | then it will be comfortable to use.
| > | |
| > | | If you still want to use unsafe way to interop the unmanaged dll,
you
| > | | should alloc the memory from unmanaged memory, but your code alloc
| from
| > | the
| > | | managed memory.
| > | | You can do like this:
| > | | [DllImport("dllt est.dll",CharSe t=CharSet.Ansi)]
| > | | unsafe static extern void ReturnCharPoint er( byte *bytebuf , int
| > | | buffersize, char * charbuf, int charbufsize);
| > | |
| > | | int a = 100;
| > | | int b = 75;
| > | |
| > | | byte* bytebuf=(byte*) Marshal.AllocCo TaskMem(a);
| > | | char* charbuf=(char*) Marshal.AllocCo TaskMem(b);
| > | |
| > | | for(int i =0; i <a; i++)
| > | | {
| > | | bytebuf[i]= Convert.ToByte( i);
| > | | }
| > | |
| > | | ReturnCharPoint er( bytebuf,a, charbuf,b);
| > | |
| > | | After ReturnCharPoint er method, in the memory window, you can see
that
| > | the
| > | | charbuf is what you want, but if you want to show it out, you should | > | | interop with some umanaged function such as printf, MessageBoxA to
| show
| > | it
| > | | out.
| > | |
| > | | Hope this helps,
| > | |
| > | | Best regards,
| > | | Jeffrey Tan
| > | | Microsoft Online Partner Support
| > | | Get Secure! - www.microsoft.com/security
| > | | This posting is provided "as is" with no warranties and confers no
| > rights.
| > | |
| > | | --------------------
| > | | | From: "Timothy Shih" <ts***@sensicas t.com>
| > | | | Subject: Need help with importing unmanaged code
| > | | | Date: Mon, 13 Oct 2003 11:08:15 -0400
| > | | | Lines: 78
| > | | | X-Priority: 3
| > | | | X-MSMail-Priority: Normal
| > | | | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | | | Message-ID: <eN************ **@tk2msftngp13 .phx.gbl>
| > | | | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | | | NNTP-Posting-Host: dsl092-093-226.bos1.dsl.sp eakeasy.net
| 66.92.93.226
| > | | | Path:
cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
| > | | | Xref: cpmsftngxa06.ph x.gbl
| > | microsoft.publi c.dotnet.langua ges.csharp:1909 96
| > | | | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| > | | |
| > | | | Hi, I am trying to figure out how to use unmanaged code using
| > P/Invoke.
| > | I
| > | | | wrote a simple function which takes in 2 buffers (one a byte
buffer,
| > | one a
| > | | | char buffer) and copies the contents of the byte buffer into the
| > | character
| > | | | pointer. The code looks like the following:
| > | | |
| > | | | #include <stdio.h>
| > | | | #include <stdlib.h>
| > | | | #include "stdafx.h"
| > | | | BOOL APIENTRY DllMain( HANDLE hModule,
| > | | | DWORD ul_reason_for_c all,
| > | | | LPVOID lpReserved
| > | | | )
| > | | | {
| > | | | return TRUE;
| > | | | }
| > | | |
| > | | | extern "C" __declspec(dlle xport)
| > | | | void ReturnCharPoint er(UINT8 *buffer, int buffersize, char
*charbuf,
| > int
| > | | | charbufsize) {
| > | | | int i =0;
| > | | | for(i=0; i < charbufsize; i++) {
| > | | | if(i>=buffersiz e)
| > | | | return;
| > | | | //printf("BYTE %d: %d\r\n",i,buffe r[i]);
| > | | | charbuf[i] = buffer[i];
| > | | | //printf("CHAR %d: %c\r\n",i,charb uf[i]);
| > | | | }
| > | | | }
| > | | |
| > | | | Now I try to import that function with the following line in a C# | > | project:
| > | | |
| > | | | [DllImport("DllS hell")]
| > | | | unsafe static extern void ReturnCharPoint er(byte *buffer, int
| > | buffersize,
| > | | | char *charbuf, int charbufsize);
| > | | |
| > | | | I use this in the following function:
| > | | |
| > | | | unsafe private void ParseBuffer()
| > | | | {
| > | | | int a = 100;
| > | | | int b = 75;
| > | | | byte[] bytebuf = new byte[a];
| > | | | char[] charbuf = new char[b];
| > | | | for(int i =0; i <a; i++)
| > | | | {
| > | | | bytebuf[i]= Convert.ToByte( i);
| > | | | }
| > | | | fixed(byte *bbuf=bytebuf)
| > | | | fixed(char *cbuf = charbuf)
| > | | | ReturnCharPoint er(bbuf,a,cbuf, b);
| > | | | for(int i =0; i < b; i++)
| > | | | {
| > | | | Console.Write(c harbuf[i] + ", ");
| > | | | }
| > | | |
| > | | | }
| > | | |
| > | | | My problem is the following: I can import and run the code just
| fine.
| > | | | However, when the function exits, the data in the character buffer | is
| > | not
| > | | | what is expected! The byte buffer contains the numbers 0-99, the
| > | character
| > | | | buffer, whose size is 75 contains the following : [0] = 256, [1] = | > 770,
| > | | [2]
| > | | | = 1284, [3] = 1798.... etc. The rest of the array is incremented
at
| > the
| > | | same
| > | | | rate until index 37 which contains the value "74". Since 74 should | be
| > | the
| > | | | last entry in the buffer, i suspect that the data is somehow being | > | | squashed
| > | | | on the way into or out of the unmanaged C dll. Also I was unable
to
| > use
| > | | | printf in the dll as it threw the following compiler error:
| > | | | "error C3861: 'printf': identifier not found, even with
| > | argument-dependent
| > | | | lookup"
| > | | |
| > | | | If anyone has any clue as to what is going on, it would be greatly | > | | | appreciated!!
| > | | |
| > | | | Thanks,
| > | | | Tim
| > | | |
| > | | |
| > | | |
| > | | |
| > | | |
| > | |
| > | |
| > |
| > |
| >
|
|
|

Nov 15 '05 #8

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

Similar topics

9
4034
by: Edward S | last post by:
I budget for a Project in an Excel sheet as illustrated below. The months below are usually a 2 year period i.e. 24 months, though it could be over 24 months depending upon a Project. I then need to input this in an Access database, where I do a comparison with the Actual cost. The table “TblBudget” in Access is made of 4 fields, namely: (1) CostElement (2) CostCenter (3) Month (4) Amount$. At the moment this method is very cumbersome....
0
1173
by: Stephen Horne | last post by:
I've only very recently started with .NET, and need to use some existing container libraries in new code. I'd like to minimise the amount of porting, but of course these containers are not designed to work in a .NET managed way. They make extensive use of pointer arithmetic, placement new and various other techniques that are obviously incompatible with managed code. If possible, I'd like to find a way of storing managed objects within...
1
1401
by: Grant Frisken | last post by:
I have a function in a Managed C++ base class which looks something like: public __gc class Foo { virtual A* Bar(B* b, C*c); }; where A is an unmanaged class and B and C are managed classes. If I create a derived class in the same DLL as class Foo and override
7
6523
by: Entwickler | last post by:
hello, i have the following problem. i want to write a code that enables the user to call functions from a unmanaged code .dll at running time . so i have to use the late binding . i tried the following code but it doesn't walk : Assembly assemblyInstance = Assembly.LoadFrom(@"C:\myData\UnmanagedCode.dll"); i don't know if somebody can help me . Regards,
3
1465
by: Kamen | last post by:
Hi, I am writing a software under .NET, what I need is a way to use my assemblies from within unamanaged c++ or VB6 projects. Is there a way that can help me import .NET assemblies within unmanaged code. Is there a way to create a single component that is so easy to embed in any project such as ActiveX? Thanks, Kamen
22
9245
by: SQACSharp | last post by:
I'm trying to get the control name of an editbox in another window. The following code set the value "MyPassword" in the password EditBox but it fail to return the control name of the EditBox. I'm sure the problem is the way i'm using the sendmessage API, the return string and the lParam return 0....is anybody have a clue? any sendmessage api expert here? public static extern Int32 FindWindow(String lpClassName,String
5
1911
by: akash | last post by:
I'm having problems calling an unmanaged class from a managed wrapper. I suspect I'm missing something obvious, as I'm unfamiliar with C++ and classes are very simple. My unmanaged class is as follows: // Unmanaged.h #pragma once namespace Unmanaged { class __declspec(dllexport) UnmanagedClass { // TODO: Add your methods for this class here.
4
6661
by: Duncan Smith | last post by:
I have a VS2005 C++ MFC project which #imports a type library. The goal is to introduce some managed code eventually, but for starters I just need to set the /clr compiler option and build the project. The type library is imported like so: #import <LtipClient.tlbno_namespace named_guids With the /clr switch on, I got lots of LNK2028 errors, so I added the following directive to the top of each cpp source file
1
1142
by: raghaw | last post by:
Hi, I have a Project implemented in unmanaged C++ .There are various header files of it.I want to know how to use the header files of this project into my one of C++/CLI project???
0
8761
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8672
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4434
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.