Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript calls C++ code with pointer to struct #638

Open
lilianglaoding opened this issue Jun 3, 2023 · 0 comments
Open

JavaScript calls C++ code with pointer to struct #638

lilianglaoding opened this issue Jun 3, 2023 · 0 comments

Comments

@lilianglaoding
Copy link

hello, everyone, I want to call C++ code in JavasSript. After I called the initialize funtion, the callback funtion is a parameter of the initilialize funtion, the callback fution will be registered, and after some operations, the C++ code will call the callback funtion the definition is below:
C++ Struct definition:
`#define MAX_EXTRA_MESSAGE_LEN 1024

struct Message {
unsigned int verison;
int sessionId;
unsigned int messageId;
unsigned int errorCode;
char extraMessage[MAX_EXTRA_MESSAGE_LEN];
union {
DistributedBundle bundle;
struct {
unsigned int dataSize;
void
data;
};
};
};
int Honor_Distributed_Initialize(int appId, const char* processName, int nameLen, DISTRIBUTED_PROC lpfnProc)
int Honor_Distributed_Initialize(int appId,const char* processName,int nameLen,DISTRIBUTED_PROC lpfnProc)
`

callback definition:
typedef void (* DISTRIBUTED_PROC)(const Message* msg);

my code is below:
`
const distributedBundle = ref.types.void
const distributedBundlePtr = ref.refType(distributedBundle)

const distributedMessage = Struct({
'version': ref.types.uint,
'sessionId': ref.types.int,
'messageId': ref.types.uint,
'errorCode': ref.types.uint,
'extraMesssage': refArray(ref.types.char, 1024),
// 'bundleData': messageUnion,
'bundle': distributedBundlePtr,
})
const distributedMessagePtr = ref.refType(distributedMessage)
distributedServiceLibrary = ffi.Library(resolvePath('build-win/dll/DistributedKit.dll'), {
'Honor_Distributed_Initialize': ['int', ['int', 'string', 'int', 'pointer']],
......

const callback = ffi.Callback('void', [distributedMessagePtr], function(messageInfo: any) {
Logger.info('[distributed service]: ffi.Callback called')
messageInfo.type = distributedMessagePtr
// const msg = ref.alloc(distributedMessagePtr, messageInfo)
Logger.info([distributed service]: version ${messageInfo.deref().version})
Logger.info([distributed service]: sessionID ${messageInfo.deref().sessionId})
Logger.info([distributed service]: messageId ${messageInfo.deref().messageId})
I registered the callback below:const initServiceResult = distributedServiceLibrary.Honor_Distributed_Initialize(appid, packageName, packageLen, callback)`

The Question is: when the callback is called, I can not get data from messageInfo, and the logger is "version undefined, sessionID undefine...", so how to code the callback funtion? thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant