473,398 Members | 2,165 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,398 software developers and data experts.

Issue Passing string from C# to C++ dll method and manipulating it.

Hi,
I have created a win32 dll with an exported method.

extern "C" __declspec(dllexport) void LocateAddress(struct stLocateAddressParam arrAddressParam,int nClientType, LPTSTR outptr);

In this method i try to copy some value to the lpstr

strcpy(outptr,ptrTemp);

When I call this method from C++, it works fine. But when I try to call it from
C# using this syntax

[DllImport("xxx.dll", SetLastError = false)]
public static extern void LocateAddress(stLocateAddressParam locatAddressParam, int nClientType,StringBuilder s);

StringBuilder s = new StringBuilder(256);
LocateAddress(stLctAddrParam, 1, s);

the dll crashes at the point of copy( I found after debugging). I do pass other variables like a struct
but they are only input parameters and work well. If I comment the copy line
it works fine. I need to pass the output in a string to C#. From what I know,
it can be done either returning a string or passing a buffer as indicated at

http://msdn2.microsoft.com/en-us/library/s9ts558h.aspx

Am I missing something?

Thanks,
Peeyush
Jan 24 '08 #1
3 5086
radcaesar
759 Expert 512MB
Here u go..

http://www.csharphelp.com/archives/archive63.html
Jan 24 '08 #2
Here u go..

http://www.csharphelp.com/archives/archive63.html
That link doesent work .

But I investigated further in the meanwhile and checked that
the problem was when my C++ method signature contains both a struct
and an LPSTR ( pass by value and pass by reference??)

If
-I change the method to LocateAddress(struct stLocateAddressParam arrAddressParam,int nClientType);

- make Global variable in the dll class to store the LPSTR
- make another getsomething method to get this stored value
immediately after method call and call it, there is no crash.

I missed the crash message earlier , it says attempt to read or write protected memory. memory is corrupt.

Thanks,
Peeyush
Jan 24 '08 #3
trying again the link worked. :-)
Jan 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mark | last post by:
hi, how can i pass state for each user through pages without using session,cookies,hidden form fields,querystring or database, which knowledge base article 175167 refers? i don't want to build a...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
4
by: B Squared | last post by:
I'm trying to pass a filename (which is a jpeg image) to a php function / file so that it will display. I know that its simple to get PHP to display an image hardcoding in the filename. For...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
4
by: Paul | last post by:
Hi, I've been struggling with this today, I'm developing a DotNet2.0 website in C# that needs to call a long running data query. Obviously this is a good candidate for an Asynchronous call, so...
4
by: Deckarep | last post by:
Hello fellow C# programmers, This question is more about general practice and convention so here goes: I got into a discussion with a co-worker who insisted that as a general practice all...
10
by: Stephany Young | last post by:
When one uses the System.Diagnostics.Process.Start method to launch a common or garden Console application, one can set the WindowStyle property of the StartInfo object to ProcessWindowStyle.Hidden...
4
by: tim.cavins | last post by:
I have a GridView populated by an ObjectDataSource. I am having issues passing the parameters to the objectdatasource. I have verified that the method is being called but none of the parameters...
11
by: =?Utf-8?B?U3VqZWV0?= | last post by:
If there are long strings (like 1MB or 2MB) is it more performant to pass those by ref to methods or by value?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
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
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,...
0
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...

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.