Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Using an array that is allocated in a native dll?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ole - 04 Nov 2006 12:33 GMT
Hi,

I have a native dll that has a byte array to which I want a pointer to in my
C# application but I will need some help to do that.

the dll exports this function:

void DllFunction (PBYTE pArray, DWORD dwLen)
{
....
}

How should I specify the PBYTE parameter in my Dllimport?

In my C# code I would like to use the imported array like:
ByteVariable = ImportedArray[5];

Is that possible - and if then how? If not - what would you suggest to do
instead?

Thanks
Ole
Ole - 04 Nov 2006 13:06 GMT
> void DllFunction (PBYTE pArray, DWORD dwLen)
> {
> ....
> }

Should ofcourse have been:
void DllFunction (PBYTE pArray, DWORD *dwLen)

Ole
Mattias Sjögren - 05 Nov 2006 15:12 GMT
>Should ofcourse have been:
>void DllFunction (PBYTE pArray, DWORD *dwLen)

If you want to return a pointer to the byte array, it has to be

void DllFunction (PBYTE *pArray, DWORD *dwLen)

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Ole - 05 Nov 2006 18:23 GMT
> >Should ofcourse have been:
>>void DllFunction (PBYTE pArray, DWORD *dwLen)
>
> If you want to return a pointer to the byte array, it has to be
>
> void DllFunction (PBYTE *pArray, DWORD *dwLen)

Thanks. I was thinking of passing a pointer to the unmanaged function and
then in that function let the pointer point to the Byte Array that I'm
interested in, but how do I declare and use the pointer in C# ?

Thanks
Ole
Selvin - 24 Nov 2006 16:24 GMT
> Thanks. I was thinking of passing a pointer to the unmanaged function and
> then in that function let the pointer point to the Byte Array that I'm
> interested in, but how do I declare and use the pointer in C# ?
>
> Thanks
> Ole

first way alloc memory in C# and pass it to unmanaged dll to fill data
in C#

using System;
using System.Runtime.InteropServices;

[DllImport("native.dll")]
public static extern bool DllFunction(IntPtr data, int len);

void somefunction()
{
int len = 70;
IntPtr ptr = Marshal.AllocHGlobal(len);
DllFunction(ptr, len);
byte[] returnbytes = new byte[len];
Marshal.Copy (returnbytes, 0, ptr, len);
Marshal.FreeHGlobal(ptr);
}

in C/CPP

BOOL DllFunction(PBYTE data, DWORD len)
{
unsigned char* mdata = (unsigned char*)data;
for(int  i = 0; i < len; i ++)
{
 mdata[i] = 44; // fill the data
}
}

second way: alloc in unmanaged and fill there back to managed copy and free
in unmanaged
in C#

using System;
using System.Runtime.InteropServices;

[DllImport("native.dll")]
public static extern bool DllFunction(ref IntPtr data, ref int len);
[DllImport("native.dll")]
public static extern bool DllFunctionFree(IntPtr data);

void somefunction()
{
int len = 0;
IntPtr ptr = IntPtr.Zero;
byte[] returnbytes = null;
if(DllFunction(ref ptr, ref len))
{
 returnbytes = new byte[len];
 Marshal.Copy (returnbytes, 0, ptr, len);
 DllFunctionFree(ptr);
}
}

in C/CPP

BOOL DllFunction(PBYTE* data, DWORD* len)
{
DWORD mlen = 255;
*len = mlen;
unsigned char* mdata =  (unsigned char*)GlobalAlloc(GMEM_FIXED, mlen);
for(int  i = 0; i < mlen; i ++)
{
 mdata[i] = 44; // fill the data
}
data = (PBYTE)mdata;
if(somethinggonewrongwithfilling)
{
 GlobalFree((HGLOBAL)mdata);
 return FALSE;
}
return TRUE;
}

void DllFunctionFree(HGLOBAL data)
{
GlobalFree(data);
}

pozdrawiam

Przemek Sulikowski
Selvin - 24 Nov 2006 16:31 GMT
<ciach>

wrong Marshal.Copy ...
should be Marshal.Copy (ptr, returnbytes, 0, len);

pozdrawiam

Przemek Sulikowski

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.