Click here to Skip to main content
15,559,491 members
Home / Discussions / C#
   

C#

 
SuggestionRe: SSH Connection to Cisco Router Pin
Richard MacCutchan8-Feb-14 6:28
mveRichard MacCutchan8-Feb-14 6:28 
GeneralRe: SSH Connection to Cisco Router Pin
sikas_Cisco8-Feb-14 6:34
sikas_Cisco8-Feb-14 6:34 
GeneralRe: SSH Connection to Cisco Router Pin
Richard MacCutchan8-Feb-14 6:37
mveRichard MacCutchan8-Feb-14 6:37 
GeneralRe: SSH Connection to Cisco Router Pin
dharmegh9-Feb-14 20:23
dharmegh9-Feb-14 20:23 
QuestionAttributes Help: Defining an item list in an attribute Pin
helkhoury7-Feb-14 21:29
helkhoury7-Feb-14 21:29 
QuestionUsing of Abstract class and Interface class Pin
Rupaswathi7-Feb-14 20:06
Rupaswathi7-Feb-14 20:06 
AnswerRe: Using of Abstract class and Interface class Pin
Richard MacCutchan7-Feb-14 23:10
mveRichard MacCutchan7-Feb-14 23:10 
QuestionC# dll import Pin
Member 42110407-Feb-14 6:40
Member 42110407-Feb-14 6: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 10:56
professionalRichard Andrew x647-Feb-14 10:56 
GeneralRe: C# dll import Pin
Member 42110407-Feb-14 11:19
Member 42110407-Feb-14 11:19 
AnswerRe: C# dll import Pin
Richard Andrew x647-Feb-14 13:41
professionalRichard Andrew x647-Feb-14 13:41 
QuestionIs it possible to impelement Voice Recognition in C# Web Applicaion? Pin
PALANI KUMAR.A6-Feb-14 23:25
PALANI KUMAR.A6-Feb-14 23:25 
AnswerRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Pete O'Hanlon7-Feb-14 0:06
mvaPete O'Hanlon7-Feb-14 0:06 
AnswerRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Snehasish_Nandy7-Feb-14 0:06
professionalSnehasish_Nandy7-Feb-14 0:06 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Marco Bertschi7-Feb-14 0:08
professionalMarco Bertschi7-Feb-14 0:08 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Snehasish_Nandy7-Feb-14 0:10
professionalSnehasish_Nandy7-Feb-14 0:10 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Marco Bertschi7-Feb-14 0:11
professionalMarco Bertschi7-Feb-14 0:11 
AnswerRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Keith Barrow7-Feb-14 0:11
professionalKeith Barrow7-Feb-14 0:11 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Pete O'Hanlon7-Feb-14 0:37
mvaPete O'Hanlon7-Feb-14 0:37 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Keith Barrow7-Feb-14 1:03
professionalKeith Barrow7-Feb-14 1:03 
Questionusb dongle software for c# Pin
Member 99539286-Feb-14 23:19
Member 99539286-Feb-14 23:19 
AnswerRe: usb dongle software for c# Pin
Marco Bertschi6-Feb-14 23:58
professionalMarco Bertschi6-Feb-14 23:58 
QuestionViewing the data enterd in the SQL Server Database for a specifed Time frame. Pin
Mohan Subramani6-Feb-14 23:15
Mohan Subramani6-Feb-14 23:15 
AnswerRe: Viewing the data enterd in the SQL Server Database for a specifed Time frame. Pin
OriginalGriff6-Feb-14 23:19
mveOriginalGriff6-Feb-14 23:19 
GeneralRe: Viewing the data enterd in the SQL Server Database for a specifed Time frame. Pin
Shameel7-Feb-14 0:43
professionalShameel7-Feb-14 0:43 

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.