0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EmscriptenによるEM_JS引数のコールバック関数でstd::string代入

Posted at

備忘録です。

#include <iostream>
#include <cstdio>
# include <emscripten.h>
#include <string>

typedef void (*CallBack)(char*, void*);

EM_JS(void, prompt_name, (CallBack funcPtr, void *HogeStr), {
  var str = "Hoge";
  var len = lengthBytesUTF8(str) + 1;
  var heap = _malloc(len);
  stringToUTF8(str, heap, len);
  
  Module['dynCall']('vii', funcPtr, [heap,HogeStr]);
});

void printMessage(char* message, void *str) {
    printf("%s\n", message);
    std::string *Str;
    Str=(std::string *)str;
    *Str=message;
    free(message);
}

int main() {
    std::string HogeStr="未変更";
  prompt_name(&printMessage, &HogeStr);
  std::cout << HogeStr << std::endl;
  printf("%s",HogeStr.c_str());
  return 0;
}

コンパイルオプションに
-s EXTRA_EXPORTED_RUNTIME_METHODS=['dynCall']
が必要です。

引用。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?