473,796 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# DLL marshal struct* : MarshalAs question

Mo
Hi,

i am trying to call unmanaged C++ DLL in my C# application
i converted all the structures to be .net compatible (e.g TCHAR to
string (including Marshal As)
one of the functions i try to call is defined in the DLL
bool hhpConnect(Conn ection_tag* ptag, PTCHAR msg);

where Connection_tag is defined as
typedef Connection_tag
{
.......
}

when i try to call it in C#, i use [DllImport("MyDL L.dll",
CharSet=CharSet .Auto)]

i am not sure how to call the Pointer to a structure? should i use
public static extern bool hhpConnect([Ref,
MarshalAs(Unman agedType.Struct )]
ref Connection_tag ptag,[MarshalAs(Unman agedType.ByValT Str)] msg);

i am confused, the *ptag would hold the connection setup parameters..?
also, i think i need to MRashal the string msg too?
how this be different if *ptag was to be changed in the hhpConnect?

Thanks ALL

Mo
ma******@hotmai l.com
Nov 16 '05 #1
2 9889
i am not sure how to call the Pointer to a structure? should i use
public static extern bool hhpConnect([Ref,
MarshalAs(Unma nagedType.Struc t)]
ref Connection_tag ptag

You're right that you should use ref Connection_tag as the parameter
type. But there's no RefAttribute, and you shouldn't use
UnmanagedType.S truct here (you don't need any MarshalAs attribute).

,[MarshalAs(Unman agedType.ByValT Str)] msg);
Leave out the MarshalAs attribute here too, and add the parameter type
string.

i am confused, the *ptag would hold the connection setup parameters..?
Can't say without knowing more about the API you're calling.

how this be different if *ptag was to be changed in the hhpConnect?


Changed in what way?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Mo
Mattias Sjögren <ma************ ********@mvps.o rg> wrote in message news:<uw******* ******@TK2MSFTN GP12.phx.gbl>.. .
i am not sure how to call the Pointer to a structure? should i use
public static extern bool hhpConnect([Ref,
MarshalAs(Unma nagedType.Struc t)] ref Connection_tag ptag

You're right that you should use ref Connection_tag as the parameter
type. But there's no RefAttribute, and you shouldn't use
UnmanagedType.S truct here (you don't need any MarshalAs attribute).

,[MarshalAs(Unman agedType.ByValT Str)] msg);


Leave out the MarshalAs attribute here too, and add the parameter type
string.

i am confused, the *ptag would hold the connection setup parameters..?


Can't say without knowing more about the API you're calling.

how this be different if *ptag was to be changed in the hhpConnect?


Changed in what way?

Mattias


Hi Mattias,
thanks for the help.
I assume if the *ptag structure has to be changed in that function ,
passing it as a ref should be fine (thats what i meant)!!
this leads me to another question, when do i have to use
hhpconnect ([In (or Out),MarshalAs( UnmanagedType.S truct)] ref
Connection_tag ptag) ....? cause i have seen some examples like this
with little info about it.

Take care
Mo
Nov 16 '05 #3

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

Similar topics

1
3976
by: alfacom | last post by:
Hi, I have two C++ structures like these : typedef struct answer_series_item { int32_t contract_size_i; int32_t price_quot_factor_i; char ins_id_s ; char isin_code_s ; uint8_t suspended_c;
0
8776
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR pchMessage; // decoded message data TCHAR chCodeID; // AIM Id of symbology TCHAR chSymLetter; // HHP Id of symbology
7
5966
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR pchMessage; // decoded message data TCHAR chCodeID; // AIM Id of symbology TCHAR chSymLetter; // HHP Id of symbology
2
11640
by: zDog | last post by:
I found a lot of information on passing data from C# to a C++ dll What I cannot find is a way to return C++ structs of TCHAR string data back to the C# managed code! typedef struct // C++ data that needs to be returned to the caller (C#) { short snOperParams; TCHAR szNFPath; TCHAR szSSFile; TCHAR szExeFile;
5
4320
by: Daniel Brown | last post by:
I am coding a managed C# wrapper for an unmanaged C DLL and I am unable to marshal a structure that contains an array of structures. When executed, the following code throws an ArgumentException with a description “Type ErrorInjectionBuffer can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed.†I have also included snippets from the .H file. Any assistance would be greatly appreciated. ...
6
10820
by: SB | last post by:
I feel dumb to ask because I bet this is a simple question... Looking at the code below, can someone please explain why I get two different values in my Marshal.SizeOf calls (see the commented lines)? TIA! sb
2
4859
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of singles that correspond to those in my structure detailed below. So when I load the file, all that I know for certain is that there will be some multiple of these eight singles represented in the binary data. My code below will read the data...
4
6479
by: gpg | last post by:
I am using a legacy DLL and need to marshal some structures for use in the DLL. For the most part, I have figured out my needs except for one small item. I have a structure that contain, among other items, an array of bools (not BOOL). The array is a fixed size and is contained in the structure. Should be simple ie:
0
1498
by: bumbala | last post by:
Below is my unmanaged structure: public struct LasHeader { public string FileSignature;
0
10461
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...
0
10239
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10019
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...
0
9057
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7555
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
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
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
2928
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.