This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
std::string in C#?
How can I call a function of a C++ DLL that accepts a parameter of type stringstream from C#?
Is there a way to convert from a c++ std:string to C# System.String? I'm calling a function from a C++ dll that takes a std:string as input. Is there a simple way to do this?
C#:
[DllImport(#"MyDLL.dll")]
[return:MarshalAs(UnmanagedType.I1)]
public static extern bool myFunction([In, Out]string input);
C++:
extern "C" __declspec(dllexport) BOOL __stdcall myFunction(const std::string& input)
{
//Code is here
}
Related
This question already has answers here:
How to correctly marshal .NET Strings to std::wstrings for native code?
(3 answers)
Marshal C++ "string" class in C# P/Invoke
(3 answers)
Custom Marshaler for PInvoke with std::string
(1 answer)
Closed 12 months ago.
When I try to import the function
extern __declspec(dllexport) void SomeNativeFunction(const std::string param1, const std::string param2);
defined and declared in SomeNative.Dll into a .NET console application using
class Program
{
[DllImport("SomeNative", CallingConvention = CallingConvention.Cdecl)]
extern static void SomeNativeFuntion(string param1, string param2);
static void Main(string[] args)
{
StartEventHandling("param1", "param2");
}
}
I get the error
System.AccessViolationException' occurred in ManagedS7DosEventManagerClient.dll
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Which is probably an indication, that I need to marshal the parameters correctly.
This overview of default marshalings for strings unfortunately doesn't say anything about marshaling of standard library strings.
This question already has answers here:
How does C++ Preprocessors work?
(1 answer)
Why are preprocessor macros evil and what are the alternatives?
(8 answers)
Closed 2 years ago.
I have a class like this:
class MyClass
{
public:
void GetForm() {}
};
Now I want to call GetForm:
MyClass myobj;
myobj.GetForm();
And at compilation I get this error:
error: 'class MyClass' has no member named 'GetFormA'; did you mean 'GetForm'?
What I have found is that in a file (winspool.h), there is a definition:
#define GetForm __MINGW_NAME_AW(GetForm)
And this results in GetFormA I guess for encoding thing.
But, how can it be possible that a #define replace in a name of a function of an object?
How can resolve this?
This question already has answers here:
When to use const and const reference in function args?
(4 answers)
C++ Const Usage Explanation
(12 answers)
Closed 4 years ago.
I am trying to learn how to use the keyword const while making header and class files (using OOP). Aka learning the correct way to incorporate the keyword 'const' while making and calling the functions.
// Example.h
class Example {
public:
string getName() const;
void setName(const string aName);
private:
const string name;
};
// Example.cpp
#include "Example.h"
#include <string>;
#include <iostream>;
Example::Example();
string Example::getName() const{
return name;
// the following setter does not work
void Example::setName(const string aName){
name = aName;
}
I figured out how to declare variable and getter/setter functions, using const in the header file. Just need help in using const with setter function in class file.
// the following setter does not work
void Example::setName(const string aName){
name = aName;"
Of course it doesn't. You declared name to be const so you cannot assign to it (you can only initialize it). Remove const from name and your setter will work.
This question already has answers here:
Undef a typedef in C++?
(3 answers)
Closed 7 years ago.
To avoid confusion and possible errors, is it possible to hide or undefine a typedef.
I do a lot of c++ and java at the same time. In java, the boolean type is boolean, and in c++ it's bool. The problem is that somewhere in the windows c++ libraries, there is a : typedef unsigned char boolean; That means that, in my c++ code, I mistype the bool type as boolean, it will compile and it could cause unexpected error, because it's an unsigned char instead of a true bool.
So what can I do to hide or undefine the boolean typedef in c++?
Yes it's possible with a precompiler directive like the example below:
typedef int foo;
#define foo not_to_be_used
int main() {
foo a = 1; // error: unknown type name 'not_to_be_used'
}
Basically, this way you cancel the typedef. The code above will issue an error if foo is used below the pre-compiler definition.
Live Demo
This question already has answers here:
C variable declarations after function heading in definition [duplicate]
(3 answers)
What weird C syntax is this? [duplicate]
(3 answers)
Closed 9 years ago.
I've been given a C/C++ code that looks like this:
extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi, extrafield_local)
zipFile file;
const char* filename;
const zip_fileinfo* zipfi;
const void* extrafield_local;
{
... function body
}
Is declaring the parameters of a function like that possible? I'm getting errors from the compiler (g++).
Thanks in advance.
This is a very old-school C (pre-ANSI C syntax) way for doing things. I suggest you change it, if you own the code, to
extern int ZEXPORT zipOpenNewFileInZip3 (
zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local)
...
There are some more details here and here
That is ancient syntax for defining functions in C. It predates the first standardized version of the C language. More importantly, that syntax has never been valid C++. Since you are compiling this code (which is obviously C code) with a C++ compiler, it is failing.