473,785 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

interop c dll import function returns a structure containing strings

Q: How do you return a LPSTR from unmanaged code?
A: Use a StringBuilder.

What about this:
public struct CSTRUCT {
string return_value;
}

How do you get a LPSTR from unmanaged code when it is in a STRUCT?

Read on for more detail.....

Hello,

I have been getting up to speed on C# Interop the last few days.

I have a program that works fairly well in C++ but I want to port it to
C# because I am more comfortable with C#.

Here is a sample of the struct I am working with: (the actual structure
is much larger).

<code>
[ StructLayout( LayoutKind.Sequ ential, CharSet=CharSet .Ansi, Pack=2 )]
public struct CSTRUCT
{
public uint prev; /* PREVIOUS */
public uint curr; /* CURRENT record pointers */
public uint next; /* NEXT */
public short list_type;
[ MarshalAs( UnmanagedType.B yValTStr, SizeConst=34 )]
public string input_data;
[ MarshalAs( UnmanagedType.B yValTStr, SizeConst=34 )]
public string return_data;
}
</code>

I pass this to a function defined like so (assume that some fields in
CSTRUCT are for internal use by the API. In this example, input_data is
a string that I need to assign a value to for the work to be done
properly):

<code>
[ DllImport( "oldc.dll", CharSet=CharSet .Ansi )]
public static extern int doWork(int a_number, ref CSTRUCT a_record);
</code>

Then I wait for the dll to raise an event. I need to call another
function to determine if doWork succeeded and what the contents of
a_record.return _data is.

<code>
[ DllImport( "oldc.dll", CharSet=CharSet .Ansi )]
public static extern int workDone(ref EVENT an_event, out CSTRUCT
updated_record) ;
</code>

Note that the API in oldc.dll maintains an internal copy of a_record.
When workDone is called, you pass in an initialised CSTRUCT variable
and workDone fills in the return_data member. (an_event is a structure
which tells the api which record to return).

My problem is this:
The code compiles (not yet, keep reading)
CLR does not raise an exception (whew! should be okay, right?)
Oops, a_record is not updated by the function call (does anybody sense
trouble?)

If I do a debug trace on the C++ code in my other project, I can see
that the structure contains unintialised data before the function call,
then afterward, return_data is correctly populated.

When I do a similar debug trace in C#, the struct is initialised to
null values (0 for integral types, "" for string types) before the
function call, and is the same after the function call.
If I do not destroy a_record after the call to doWork, and pass that to
workDone, in this case the data also is not changed, eg, input_data is
still populated, but return_value has not changed. Is this the standard
behaviour for .Net? Is there another way to accomplish this?

Things tried: changing updated_record from "out" to "ref" - no change.
Changing "ref CSTRUCT" to "CSTRUCT" (and making CSTRUCT a class instead
of a struct) - no change.
Examining the int return value from workDone - the return value
indicates that the function succeeded.
Changing "out CSTRUCT" to "byte[]" where the byte array is initialised
to the same size as CSTRUCT - after the function call, the byte array
is unchanged, eg all items are 0. (I tried this on the doWork function
and the data was passed in correctly.)

Ideas:
1) Go back to c++ and create a separate application
2) Go back to c++, create a wrapper of some kind (dll) that I call from
C#. C++ gets the returned struct, then passes back the string values
individually to C#.

Thanks,

kagorami

Feb 7 '06 #1
0 2131

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

Similar topics

9
4964
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
3
2298
by: Jon Skeet | last post by:
I'm trying to speed up a data migration tool I'm writing that uses COM interop. Currently I'm accessing each field within a record individually, which works, but means going across the managed/unmanaged boundary a heck of a lot. The COM object I'm working with has a method which I'm sure *should* help, but I can't get it to work. It's described in the documentation as: GetDataEx method:
2
1771
by: Frank | last post by:
Hello, i have a problem with the data exchange between a managed C# application and an unmanged C++ DLL. The C# app fills some values of a structure and calls the C++ DLL. This DLL reads the filled data from the structure and fills the results into the same structure. This code above works, but sometimes the data array is empty, although the GetMemory function returns true. For example: i read 250kByte (~1000 function calls) 5 - 15 times...
0
1041
by: Andrew S. Giles | last post by:
Hello again! I am back for more information, as I am moving into trying to utilize COM to work with unmanaged code from C#. In C# I have a series of classes to encapsulate data that needs to be gathered for a report. This data is resident in an unmanaged bit of C++ code. We want to use COM to facilitate the transfer of this data to the C# environment so the report may be generated. MY plan is to develop a C# class and interface that...
1
6789
by: ivang | last post by:
Hello, All! Please help me to marshal following win32 structure: typedef struct _STARTUPINFOW { DWORD cb; LPWSTR lpReserved;
0
2488
by: MarkD | last post by:
I have an ASP.NET (VB.NET) application that calls all VB6 COM DLL via Interop. The DLL uses functionality contained in a Custom OCX Control (Also VB6) that in turn contains a standard TreeView control. The Custom OCX is not used for visual purposes, but has some functionality that we require (hence hosting an OCX in a DLL). Everything worked fine until we installed the latest service pack for each respective windows operating system:...
4
3404
by: DotNetJunkies User | last post by:
I am calling a VB6 dll from a vb.net windows application that returns an array of strings. My issue is it seems to truncate after a NULL character. For Example VB 6 is returning a string with the HEX value of 4E 31 00 00 01 00 20 20 20 20 20 00 00 00 20 20 20 31 32 30. But when it gets back to Vb.net all I have is 4E 31 or N1. Now I can return the same string as a return value of a function and I get it all. Please Help, Patrick Horn...
14
1846
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message needs to be returned to the calling code. As I see it there are a few options.
6
26330
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this one table. My problem is I have two buttons I want to use this code for for the two buttons would put the data in different tables. I have tried copying and changing a few things and nothing will work for me. The code is set up in a module and then I...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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
10325
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
10091
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
9950
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
7499
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.