473,396 Members | 1,921 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,396 software developers and data experts.

C declarations in C#, continued

Hi,

I finally got this C function to be called correctly from C#.

char * version_chk( const char *)

I needed to do the following:

[DllImport( "test.dll",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr version_chk(
[MarshalAs(UnmanagedType.LPStr)]
string version );

string sTestVersion = "3.0f_p2";
IntPtr test = version_chk( sTestVersion );
// how do we figure out the length?
string sReturnVersion = Marshal.PtrToStringAnsi(test,7);
Console.WriteLine( "version is " + sReturnVersion ) ;

The key was the CallingConvention!

Now that I have the IntPtr, test, available, I'm trying to understand
how I can know the length instead of having to hardcode it.

Any ideas?

Thanks,

Ted
Nov 15 '05 #1
2 1094
Can you marshal in strlen()?

"Ted Sung" <te**@intex.com> wrote in message
news:86**************************@posting.google.c om...
Hi,

I finally got this C function to be called correctly from C#.

char * version_chk( const char *)

I needed to do the following:

[DllImport( "test.dll",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr version_chk(
[MarshalAs(UnmanagedType.LPStr)]
string version );

string sTestVersion = "3.0f_p2";
IntPtr test = version_chk( sTestVersion );
// how do we figure out the length?
string sReturnVersion = Marshal.PtrToStringAnsi(test,7);
Console.WriteLine( "version is " + sReturnVersion ) ;

The key was the CallingConvention!

Now that I have the IntPtr, test, available, I'm trying to understand
how I can know the length instead of having to hardcode it.

Any ideas?

Thanks,

Ted

Nov 15 '05 #2
Now that I have the IntPtr, test, available, I'm trying to understand
how I can know the length instead of having to hardcode it.

Any ideas?


If the string is zero terminated, you can simply use the
PtrToStringAnsi overload that doesn't take a length parameter. It will
read the string up to the null terminator.

Mattias

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

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

Similar topics

9
by: Michael Tobis | last post by:
Summary of my understanding of a recent interesting thread: General usage has "declaration" meaning "statement which does not generate executable bytecode but merely affects the compiler". My...
0
by: Rikard Land | last post by:
I try to model a data definition language in XML. It can be seen as C without any executable statements other than variable assignments. I want to allow for: (1) type declarations ("structs" in...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
0
by: Nunya Biznas | last post by:
I have a two column report that needs to have the word "continued" appear at the top of the 2nd column if the detail records are part of the group from the previous column. I have tried using a...
14
by: Arthur J. O'Dwyer | last post by:
Well, I'm trying to write that program that was requested a few weeks back, the one that could take struct definitions and create portable functions to read and write those structs. Hence the...
28
by: Michael B. | last post by:
I tend to use rather descriptive names for parameters, so the old style of declaration appeals to me, as I can keep a declaration within 80 chars: void * newKlElem...
1
by: Chris | last post by:
This may be more of a Visual Studio question but those groups seem to be full of unrelated stuff so hopefully this might be the right place. I have a class (no associated aspx file) which handles...
4
by: andreas.w.h.k. :-\) | last post by:
How do I change the address location in the wsdl <wsdl:port name="SearchSoap12" binding="tns:SearchSoap12"> <soap12:address location="http://searchservices/engine/search.asmx" /> </wsdl:port> ...
10
by: Andy Fish | last post by:
hi, I have an XSLT which is producing XML output. many of the nodes in the output tree contain namespace declarations for namespaces that are used in the source document even though they are...
7
by: aspineux | last post by:
Hi I read the PEP 3117 about the new "Postfix type declarations" in Python3000. THIS PEP as been REJECTED ! But ... The notation in the PEP is very ugly ! This make python code more...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.