473,378 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

P/Invoke Problem

Hello,

I have the following C functions exported from a DLL:

void copyString2(const char * sSrcBuf, long nSrcBufSize, char ** sDestBuf);
void freeString(char * sBuf);

The idea is to copy the source buffer into the dynamically allocated
destination buffer returning the pointer to the buffer to C# then pushing the
pointer back to the DLL for deallocation.

I'm having a tough time declaring the appropriate type for the char **
sDestBuf parameter. I've been trying to use various combination of IntPtr
declarations but no matter what I define at runtime the pointer passed to the
C library is NULL for the sDestBuf.

For example:
[DllImport("nativelibtest.dll", CharSet = CharSet.Ansi)]
public static extern unsafe
bool copyString2(string sSrcStr, long nSrcSize, IntPtr * sDestBuf);

IntPtr pPtr = new IntPtr();

bool bSuccess = NativeLibTest.copyString2(sMsg, sMsg.Length, &pPtr);

In the debugger, &pPtr certainly has an address but that address is not
passed to the DLL.

Any help would be appreciated.

Oct 9 '07 #1
6 1659
Mike,

You don't need unsafe code to do this. You can just pass the last
parameter as a ref IntPtr, and then the IntPtr should have a value written
to it. You then have to pass that IntPtr to the static PtrToStringAnsi
method on the Marshal class to get a String instance you can work with.

Of course, you have to make sure you pass the IntPtr back to freeString.
You can just declare the parameter of that function as an IntPtr, and it
should work fine.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Mike M" <Mike M@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...
Hello,

I have the following C functions exported from a DLL:

void copyString2(const char * sSrcBuf, long nSrcBufSize, char **
sDestBuf);
void freeString(char * sBuf);

The idea is to copy the source buffer into the dynamically allocated
destination buffer returning the pointer to the buffer to C# then pushing
the
pointer back to the DLL for deallocation.

I'm having a tough time declaring the appropriate type for the char **
sDestBuf parameter. I've been trying to use various combination of IntPtr
declarations but no matter what I define at runtime the pointer passed to
the
C library is NULL for the sDestBuf.

For example:
[DllImport("nativelibtest.dll", CharSet = CharSet.Ansi)]
public static extern unsafe
bool copyString2(string sSrcStr, long nSrcSize, IntPtr * sDestBuf);

IntPtr pPtr = new IntPtr();

bool bSuccess = NativeLibTest.copyString2(sMsg, sMsg.Length, &pPtr);

In the debugger, &pPtr certainly has an address but that address is not
passed to the DLL.

Any help would be appreciated.
Oct 10 '07 #2
>void copyString2(const char * sSrcBuf, long nSrcBufSize, char ** sDestBuf);
[...]
>[DllImport("nativelibtest.dll", CharSet = CharSet.Ansi)]
public static extern unsafe
bool copyString2(string sSrcStr, long nSrcSize, IntPtr * sDestBuf);
nSrcSize should be an int in C# to match the long in C. And how did
the void return type became bool on the C# side?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 10 '07 #3
Oh, that was just me switching back and forth between copy/paste and
typing...the both should have been bool.

Oct 10 '07 #4
Nope, not having too much luck.

Anybody have any p/invoke samples calling a library function with a
parameter using 2 level of indirection where the C function allocates memory
and returns the pointer back to C#? Then the C# caller is responsible for
calling a "free" function exposed by the native library.

Oct 10 '07 #5
"Mike M" <Mike M@discussions.microsoft.comwrote in message
news:2B**********************************@microsof t.com...
Nope, not having too much luck.

Anybody have any p/invoke samples calling a library function with a
parameter using 2 level of indirection where the C function allocates
memory
and returns the pointer back to C#? Then the C# caller is responsible for
calling a "free" function exposed by the native library.

// C# code
[DllImport("xxxxxx"), SuppressUnmanagedCodeSecurity] static extern void
Test(out IntPtr s);

....
IntPtr buffer;
Test(out buffer);
Console.WriteLine(Marshal.PtrToStringAnsi(buffer)) ;

....

// C code

extern "C"
{
void __declspec(dllexport) __stdcall Test(void **ptr)
{
int bufferSize = 25;
*ptr = new unsigned char(bufferSize );
strcpy_s((char*)*ptr, bufferSize, "Hello from C");
}
}

Willy.

Oct 10 '07 #6
Thank you for your time.

My problem was the bit width of the nSrcSize integer parameter in my C#
declaration. Should have been using int as Mattias pointed out earlier.

Thanks again,
Mike

Oct 11 '07 #7

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

Similar topics

1
by: John Altland | last post by:
Here is my basic problem. I have a form that executes a cpu instensive algorithm occupying the first thread. When the algorithm is executed another form pops up telling the user the progress that...
5
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the...
5
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker...
2
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method...
0
by: Paul Tomlinson | last post by:
Morning all. Let me describe my problem. Main thread creates child objects and threads and starts them Timer on the main thread (which only gets activated after the child threads have started)...
4
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
3
by: m.posseth | last post by:
Hello does someone know how i can invoke a method in the underlying thread without the usage of a window handle ?? This works perfect in a form Me.Invoke(New MethodInvoker(AddressOf...
2
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.