Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
AnswerRe: video and gps Pin
Ravi Bhavnani9-Feb-14 4:34
professionalRavi Bhavnani9-Feb-14 4:34 
QuestionGetting Data From User Pin
RebornProgrammer8-Feb-14 21:00
RebornProgrammer8-Feb-14 21:00 
AnswerRe: Getting Data From User Pin
OriginalGriff8-Feb-14 23:21
mveOriginalGriff8-Feb-14 23:21 
QuestionWrite to notepad or other windows Pin
alirezamansoori8-Feb-14 19:27
alirezamansoori8-Feb-14 19:27 
AnswerRe: Write to notepad or other windows Pin
OriginalGriff8-Feb-14 19:58
mveOriginalGriff8-Feb-14 19:58 
QuestionHow to Fetch UserName & Current Date automatically. Pin
Mohan Subramani8-Feb-14 7:01
Mohan Subramani8-Feb-14 7:01 
AnswerRe: How to Fetch UserName & Current Date automatically. Pin
OriginalGriff8-Feb-14 7:32
mveOriginalGriff8-Feb-14 7:32 
AnswerRe: How to Fetch UserName & Current Date automatically. Pin
Dave Kreskowiak8-Feb-14 9:22
mveDave Kreskowiak8-Feb-14 9:22 
AnswerRe: How to Fetch UserName & Current Date automatically. Pin
Mycroft Holmes8-Feb-14 13:35
professionalMycroft Holmes8-Feb-14 13:35 
AnswerRe: How to Fetch UserName & Current Date automatically. Pin
Richard Deeming10-Feb-14 1:37
mveRichard Deeming10-Feb-14 1:37 
Questionhow can i get min value in datatable with distinct items using loops Pin
yahiaalaa8-Feb-14 5:49
yahiaalaa8-Feb-14 5:49 
AnswerRe: how can i get min value in datatable with distinct items using loops Pin
TnTinMn8-Feb-14 6:54
TnTinMn8-Feb-14 6:54 
AnswerRe: how can i get min value in datatable with distinct items using loops Pin
Mycroft Holmes8-Feb-14 13:31
professionalMycroft Holmes8-Feb-14 13:31 
AnswerRe: how can i get min value in datatable with distinct items using loops Pin
Md Nazmoon Noor13-May-14 3:21
Md Nazmoon Noor13-May-14 3:21 
QuestionSSH Connection to Cisco Router Pin
sikas_Cisco8-Feb-14 4:36
sikas_Cisco8-Feb-14 4:36 
SuggestionRe: SSH Connection to Cisco Router Pin
Richard MacCutchan8-Feb-14 5:28
mveRichard MacCutchan8-Feb-14 5:28 
GeneralRe: SSH Connection to Cisco Router Pin
sikas_Cisco8-Feb-14 5:34
sikas_Cisco8-Feb-14 5:34 
GeneralRe: SSH Connection to Cisco Router Pin
Richard MacCutchan8-Feb-14 5:37
mveRichard MacCutchan8-Feb-14 5:37 
GeneralRe: SSH Connection to Cisco Router Pin
dharmegh9-Feb-14 19:23
dharmegh9-Feb-14 19:23 
QuestionAttributes Help: Defining an item list in an attribute Pin
helkhoury7-Feb-14 20:29
helkhoury7-Feb-14 20:29 
QuestionUsing of Abstract class and Interface class Pin
Rupaswathi7-Feb-14 19:06
Rupaswathi7-Feb-14 19:06 
AnswerRe: Using of Abstract class and Interface class Pin
Richard MacCutchan7-Feb-14 22:10
mveRichard MacCutchan7-Feb-14 22:10 
QuestionC# dll import Pin
Member 42110407-Feb-14 5:40
Member 42110407-Feb-14 5:40 
Hi,

I have a strange problem when calling one of my C++ dll functions from within C# code. Even though I have a work around but it is not making sense to me what caused the problem in the first place and would like to understand why.

I am writing below a very simple code that produces the problem:
Basically, My dll function returns a pointer to a null terminate string (const char *) that I would like to import it as a string in c# code.

The c++ DLL function :
C++
const char* ReturnSomeString()
{
    static string result="The string";
    return result.c_str();
}


The C# code:
C#
[DllImport(@"thedll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.LPStr)]
public extern static string ReturnSomeString();

public void function()
{
  string result = ReturnSomeString();
}


When I run the c# code, sometimes it works, sometimes the result is garbage and most of the times it crashes with with exception message :

An unhandled exception of type 'System.AccessViolationException' occurred in theapplication.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


As I mentioned, I have some couple work arounds but still want to understand why the above methodology does not work.

Work around 1:
---------------------
change the C++ code to not to use string class and just use the standard ansi char[] keeping the C# code unchanged.

Work around 2:
-----------------------
Keep the C++ code as is (using string class) but modify C# code:

C#
[DllImport(@"thedll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr ReturnSomeString();

public void function()
{
  string result = Marshal.PtrToStringAnsi(ReturnSomeString());
}


Please help me explaining what is wrong with the first method.

Thanks for taking the time looking into this.
AnswerRe: C# dll import Pin
Richard Andrew x647-Feb-14 9:56
professionalRichard Andrew x647-Feb-14 9:56 
GeneralRe: C# dll import Pin
Member 42110407-Feb-14 10:19
Member 42110407-Feb-14 10:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.