Click here to Skip to main content
15,920,602 members
Home / Discussions / C#
   

C#

 
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 
AnswerRe: C# dll import Pin
Richard Andrew x647-Feb-14 12:41
professionalRichard Andrew x647-Feb-14 12:41 
QuestionIs it possible to impelement Voice Recognition in C# Web Applicaion? Pin
PALANI KUMAR.A6-Feb-14 22:25
PALANI KUMAR.A6-Feb-14 22:25 
AnswerRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Pete O'Hanlon6-Feb-14 23:06
mvePete O'Hanlon6-Feb-14 23:06 
AnswerRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Snehasish_Nandy6-Feb-14 23:06
professionalSnehasish_Nandy6-Feb-14 23:06 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Marco Bertschi6-Feb-14 23:08
protectorMarco Bertschi6-Feb-14 23:08 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Snehasish_Nandy6-Feb-14 23:10
professionalSnehasish_Nandy6-Feb-14 23:10 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Marco Bertschi6-Feb-14 23:11
protectorMarco Bertschi6-Feb-14 23:11 
AnswerRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Keith Barrow6-Feb-14 23:11
professionalKeith Barrow6-Feb-14 23:11 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Pete O'Hanlon6-Feb-14 23:37
mvePete O'Hanlon6-Feb-14 23:37 
GeneralRe: Is it possible to impelement Voice Recognition in C# Web Applicaion? Pin
Keith Barrow7-Feb-14 0:03
professionalKeith Barrow7-Feb-14 0:03 
Questionusb dongle software for c# Pin
Member 99539286-Feb-14 22:19
Member 99539286-Feb-14 22:19 
AnswerRe: usb dongle software for c# Pin
Marco Bertschi6-Feb-14 22:58
protectorMarco Bertschi6-Feb-14 22:58 
QuestionViewing the data enterd in the SQL Server Database for a specifed Time frame. Pin
Mohan Subramani6-Feb-14 22:15
Mohan Subramani6-Feb-14 22:15 
AnswerRe: Viewing the data enterd in the SQL Server Database for a specifed Time frame. Pin
OriginalGriff6-Feb-14 22:19
mveOriginalGriff6-Feb-14 22:19 
GeneralRe: Viewing the data enterd in the SQL Server Database for a specifed Time frame. Pin
Shameel6-Feb-14 23:43
professionalShameel6-Feb-14 23: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.