将 javascript 字符串传递给 std::string 打印垃圾值
Passing javascript string to std::string prints garbage values
我正在玩 C++ 和 ElectronJS。我正在尝试使用 ElectronJS 和 node-ffi 将 Javascript 字符串传递给 c++ dll。除了 GetString
.
之外,其余公开的功能都运行良好
Javascript代码:
window.onload = function () {
try {
// Call *.dll with ffi
let ffi = require('ffi-napi');
// Create funtions
window.Dll = ffi.Library(".//dll//BasicMathDll.dll", {
'add': ['float', ['float', 'float']],
"subtract": ["float", ["float", "float"]],
"multiply": ["float", ["float", "float"]],
"divide": ["float", ["float", "float"]],
"GetString": ["string",["string"]]
})
console.log("DLL was loaded correctly.");
// Call C++ function Hello
//document.getElementById('hello').innerHTML = Dll.Hello()
} catch (error) {
console.error('ffi.Library', error);
document.getElementById('result').value = error;
}
}
Wrapper.cpp
std::string GetString(std::string a) {
FILE* fp;
fp = fopen("a.txt", "w+");
fprintf(fp, a.c_str());
fclose(fp);
return "The quick brown fox jumps over the lazy dog.";
}
Wrapper.h
#pragma once
#ifdef BASICMATHDLL_EXPORTS
#define BASICMATHDLL_API __declspec (dllexport)
#else
#define
#define BASICMATHDLL_API __declspec (dllimport)
#endif
#if defined(WIN32) || defined(_WIN32)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct CWrapper {
void* cObject;
}CWrapper_T;
CWrapper_T *create();
void destroy(CWrapper_T*);
float CheckTask(CWrapper_T* mObject, float x, float y, char mOperator);
EXPORT float add(float x, float y);
EXPORT float subtract(float x, float y);
EXPORT float multiply(float x, float y);
EXPORT float divide(float x, float y);
EXPORT std::string GetString(std::string);
#ifdef __cplusplus
}
#endif
我这样调用公开的 dll 函数:
function GetStr(){
// Call C++ function GetString
document.getElementById('string').innerHTML = window.Dll.GetString("Lorem ipsum dolor.");
}
生成的文本文件(a.txt)包含:
øã«~S{Säã
我认为 javascript 字符串和 c++ std::string 之间的字符编码有问题,目前我还不知道。有人有想法吗?
我尝试了 C 风格的字符串,它是 const char*
而不是 C++ std::string
,它工作正常。
以下是我所做的小调整。
Wrapper.h
EXPORT const char* GetString(const char*);
Wrapper.cpp
const char* GetString(const char* a) {
FILE* fp;
fp = fopen("a.txt", "w+");
fprintf(fp, a);
fclose(fp);
return "The quick brown fox jumps over the lazy dog.";
}
现在我的 a.txt
文件有一个从 JS 传递的正确值。
我正在玩 C++ 和 ElectronJS。我正在尝试使用 ElectronJS 和 node-ffi 将 Javascript 字符串传递给 c++ dll。除了 GetString
.
Javascript代码:
window.onload = function () {
try {
// Call *.dll with ffi
let ffi = require('ffi-napi');
// Create funtions
window.Dll = ffi.Library(".//dll//BasicMathDll.dll", {
'add': ['float', ['float', 'float']],
"subtract": ["float", ["float", "float"]],
"multiply": ["float", ["float", "float"]],
"divide": ["float", ["float", "float"]],
"GetString": ["string",["string"]]
})
console.log("DLL was loaded correctly.");
// Call C++ function Hello
//document.getElementById('hello').innerHTML = Dll.Hello()
} catch (error) {
console.error('ffi.Library', error);
document.getElementById('result').value = error;
}
}
Wrapper.cpp
std::string GetString(std::string a) {
FILE* fp;
fp = fopen("a.txt", "w+");
fprintf(fp, a.c_str());
fclose(fp);
return "The quick brown fox jumps over the lazy dog.";
}
Wrapper.h
#pragma once
#ifdef BASICMATHDLL_EXPORTS
#define BASICMATHDLL_API __declspec (dllexport)
#else
#define
#define BASICMATHDLL_API __declspec (dllimport)
#endif
#if defined(WIN32) || defined(_WIN32)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct CWrapper {
void* cObject;
}CWrapper_T;
CWrapper_T *create();
void destroy(CWrapper_T*);
float CheckTask(CWrapper_T* mObject, float x, float y, char mOperator);
EXPORT float add(float x, float y);
EXPORT float subtract(float x, float y);
EXPORT float multiply(float x, float y);
EXPORT float divide(float x, float y);
EXPORT std::string GetString(std::string);
#ifdef __cplusplus
}
#endif
我这样调用公开的 dll 函数:
function GetStr(){
// Call C++ function GetString
document.getElementById('string').innerHTML = window.Dll.GetString("Lorem ipsum dolor.");
}
生成的文本文件(a.txt)包含:
øã«~S{Säã
我认为 javascript 字符串和 c++ std::string 之间的字符编码有问题,目前我还不知道。有人有想法吗?
我尝试了 C 风格的字符串,它是 const char*
而不是 C++ std::string
,它工作正常。
以下是我所做的小调整。
Wrapper.h
EXPORT const char* GetString(const char*);
Wrapper.cpp
const char* GetString(const char* a) {
FILE* fp;
fp = fopen("a.txt", "w+");
fprintf(fp, a);
fclose(fp);
return "The quick brown fox jumps over the lazy dog.";
}
现在我的 a.txt
文件有一个从 JS 传递的正确值。