473,621 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pInvoke - arguments marshaling

pInvoke - arguments marshaling
Hi,
I am using pInvoke and the member function has signature like this in C++

int F1(ULONG handle, LPCTSTR s1, LPCTSTR s2, LPCTSTR s3, LPCTSTR s4)

so, I am using this signature in C#

public static extern int F1([MarshalAs(Unman agedType.U8)] UInt64 handle,
[MarshalAs(Unman agedType.LPTStr )]string s1,
[MarshalAs(Unman agedType.LPTStr )]string s2,
[MarshalAs(Unman agedType.LPTStr )]string s3,
[MarshalAs(Unman agedType.LPTStr )]string s4);

So, if I call it like this

F1(1234,"Earth" ,"Moon","Sun"," ");

then I am receiving these values in C++ dll
handle= 1234
s1= null
s2= Earth
s3= Moon
s4= Sun

The arguments seems to be shifted.

If I remove the ULONG from the declaration at both place, everything
works fine.
ULONG is 64 bit unsigned int. So, it should be passed as U8!! So, whats
wrong here???

Please advice!!

Ashutosh
Jun 27 '08 #1
2 3073
ULONG is a 32bit unsigned integer. long in C++ and long in C# are not the
same thing.

"Ashutosh" <sm*******@nosp am.nospamwrote in message
news:Ou******** ********@TK2MSF TNGP04.phx.gbl. ..
pInvoke - arguments marshaling
Hi,
I am using pInvoke and the member function has signature like this in C++

int F1(ULONG handle, LPCTSTR s1, LPCTSTR s2, LPCTSTR s3, LPCTSTR s4)

so, I am using this signature in C#

public static extern int F1([MarshalAs(Unman agedType.U8)] UInt64 handle,
[MarshalAs(Unman agedType.LPTStr )]string s1,
[MarshalAs(Unman agedType.LPTStr )]string s2,
[MarshalAs(Unman agedType.LPTStr )]string s3,
[MarshalAs(Unman agedType.LPTStr )]string s4);

So, if I call it like this

F1(1234,"Earth" ,"Moon","Sun"," ");

then I am receiving these values in C++ dll
handle= 1234
s1= null
s2= Earth
s3= Moon
s4= Sun

The arguments seems to be shifted.

If I remove the ULONG from the declaration at both place, everything works
fine.
ULONG is 64 bit unsigned int. So, it should be passed as U8!! So, whats
wrong here???

Please advice!!

Ashutosh

Jun 27 '08 #2
Thanks! I got confused with Visual Basic ULong!!! All this time I was
seeing VB's document in MSDN :-)

Stephen Martin wrote:
ULONG is a 32bit unsigned integer. long in C++ and long in C# are not the
same thing.

"Ashutosh" <sm*******@nosp am.nospamwrote in message
news:Ou******** ********@TK2MSF TNGP04.phx.gbl. ..
>pInvoke - arguments marshaling
Hi,
I am using pInvoke and the member function has signature like this in C++

int F1(ULONG handle, LPCTSTR s1, LPCTSTR s2, LPCTSTR s3, LPCTSTR s4)

so, I am using this signature in C#

public static extern int F1([MarshalAs(Unman agedType.U8)] UInt64 handle,
[MarshalAs(Unman agedType.LPTStr )]string s1,
[MarshalAs(Unman agedType.LPTStr )]string s2,
[MarshalAs(Unman agedType.LPTStr )]string s3,
[MarshalAs(Unman agedType.LPTStr )]string s4);

So, if I call it like this

F1(1234,"Earth ","Moon","Sun", "");

then I am receiving these values in C++ dll
handle= 1234
s1= null
s2= Earth
s3= Moon
s4= Sun

The arguments seems to be shifted.

If I remove the ULONG from the declaration at both place, everything works
fine.
ULONG is 64 bit unsigned int. So, it should be passed as U8!! So, whats
wrong here???

Please advice!!

Ashutosh

Jun 27 '08 #3

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

Similar topics

0
338
by: vijaya | last post by:
I've a problem with PInvoke.I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.Please apologise for this lenghty message and render ur kind help. The original dll function and my C# code has been given below along with the error message. Error: The return code is 0 which means the fucntion has executed successfully. But I'm not getting the correct values in the structure pborcadirentry. Note: I've tried GC...
1
348
by: Muthiah Samy | last post by:
Hi All, I am trying to call COM Component in .NET using PInvoke. I have rewritten custom IDL file in a managed language(C#). When I try to invoke one of the COM method from .NET, I am getting the following exception. System.Runtime.InteropServices.SEHException: External component has thrown an exception.
5
96480
by: VM | last post by:
What's marshalling? I've had to use it extensively for a project but I don't know what it means. I tried to look for a definition in the Internet but I couldn't find anything that would explain what it is. Is converting a C-style struct to a C# class (or struct) marshalling? And what about the function exports? Are those Marshalling too? Thanks for your help and thanks for helping me with the Dll exports (the several messages I posted in...
5
7871
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. The native DLL uses raw data buffers that are normally handed to it (via pointer) from another native function. If I'm piloting this with C#/PInvoke, how could those raw data buffers be created and immobilized? In C++/CLI, there is pin_ptr to...
1
2671
by: Bill Medland | last post by:
I am trying to do some P/Invoke custom marshaling and am looking for a little help on custom marshaling a value type. I am working based on Kate Gregory's "Arranging Custom Marshaling With P/Invoke", which is unfortunately in C++ with managed extensions; I am working in C#. As with Kate's example I want to marshal from a System.DateTime to a proprietary format. However I cannot figure out how to tell the system to box the DateTime. ...
2
7356
by: MyCrystalGift | last post by:
Hi, I have an old C++ GUI Application CPPAPP.exe that calls a C DLL library RULE.DLL through a C++ class wrapper LoadRule.CPP. Now I need to call the C DLL RULE.DLL from C# GUI application CSAPP.exe. I need help to convert LoadRule.CPP to a C++ BridgeDLL, and I need help on how to convert the call to the BridgeDLL from C#.
3
5251
by: Beorne | last post by:
I have a propertary library dll (used to drive a device) that I call from my C# code. Calling the functions from C++ is really faster than calling them in C+ +. From C++ the call is almost instantaneous, from C# is about 1.5 seconds long. For example, this command powers a device based on an interface switch and returns a message in Ack_Msg that is len long. Besides returns an error code.
3
3222
by: not_a_commie | last post by:
The CLR won't garbage collect until it needs to. You should see the memory usage climb for some time before stabilizing. Can you change your declaration to use the 'out' keyword rather than a 'ref' keyword?
2
2682
by: =?Utf-8?B?S2VsdmluIEpvaG5zb24=?= | last post by:
I am wrapping a legacy C api using C++\CLI. I will use the resulting classes from C# and VB.net. I need to pass command line arguments from the .net languages to the c api functions, so I've created a custom marshaller for the task. The problem is that I get an error stating that a conversion from type1 to type2 is not supported. I've read the MSDN documentation (along with an apress title or two), but I am still missing somthing. ...
0
8156
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,...
0
8653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8306
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
8457
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
6101
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
4065
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...
1
2587
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
1
1763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1460
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.