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.
Related
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:
Calling C++ dll from Java
(3 answers)
Closed 8 years ago.
I have one c++ dll which is previously used for c# application. now we want to use the same dll for java . i know that we can use JNI technology for this but the problem we have to use the same method signature we don't want to change the method singnature. please advise me.
One option is using JNA instead of JNI. It eliminates the need for the boilerplate native code. An example would look something like this...
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Example {
public interface NativeMath extends Library {
public bool isPrime(int x);
}
public static void main(String[] args) {
int x = 83;
NativeMath nm = (NativeMath) Native.loadLibrary("nm", NativeMath.class);
System.out.println(x + " is prime: " + nm.isPrime(x));
}
}
You don't have to change the method signature, you simply add a native method which then calls the native C++ code. Here is a simple example:
public class Someclass
{
public native void thisCallsCMethod(Someparams);
}
Now create the JNI wrappers:
javac Someclass.java
javah Someclass
This will create a Someclass.h, then you create Someclass.cpp and include the .h in it.
At this point all you have to do is write the C/C++ code for thiCallsCMethod
In the .h you'll see a method signature that you have to implement. Something along the lines of:
#include "clibraryHeader.h"
using namespace clibraryNamespace;
JNIEXPORT void JNICALL thisCallsCMethod(JNIEnv *, someparameters)
{
cout<<"Yeah C code is being called"<<endl;
someCfunction();
}
Obviously you have to massage the parameters in the JNI call, but you can create some temporary variables, then copy back the values you get from the C calls into the incoming parameters (if they need to be returned) etc.
Maybe:
#include "clibraryHeader.h"
using namespace clibraryNamespace;
JNIEXPORT void JNICALL thisCallsCMethod(JNIEnv *, someparameters)
{
cout<<"Yeah C code is being called"<<endl;
Cstruct temp;
temp1.somevar = param1.getSomeVal()
someCfunction(temp);
}
This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 8 years ago.
I'm trying to define some static constant strings in C++ and reference them from different files.
Here's how I have the information set up this far:
structClass.h
namespace test {
typedef struct customstructure{
static const std::string stringA;
} customstructure;
}
structClass.cpp
namespace test {
static const std::string customstructure::stringA = "This is String A";
}
Now I'm wondering how I would call this in a third file?
execute.cpp
void printStringA(){
printf("stringA is: %s", test::customstructure::stringA.c_str());
}
gives me a compile error that says:
undefined reference to 'test::customstructure::stringA'
In this code:
namespace test {
static const std::string customstructure::stringA = "This is String A";
}
remove the word static. In fact it is an error to have this, your compiler should give a more useful error message (although I suppose 'undefined reference' meets the requirements of "a diagnostic").
Standard reference: [class.static.data]#5 says that static data members have external linkage, however using the keyword static in the definition would specify internal linkage.
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.
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
}