1
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 1 year has passed since last update.

WindowsAPIのSendMessageで文字列を通信する方法を思いついたのでメモ

Last updated at Posted at 2024-03-29

Virtual2Dの声部分を作る過程で、VOICEVOXの音声ファイルパスを通信する必要があって、テキストを通信する方法を考えていたら思いついたのでメモします。

その方法は、文字を分割して送る方法です。

送信側

  1. 送る文字列を用意します
  2. 今回は1文字ずつで分割します。または一文字ごとに処理できるようにします
  3. まず文字列を初期化するように信号を送ります
初期化信号の例.cpp
SendMessage(hWnd, 0x0EDA, 1, 0);
/*Msgが0xEDA - 文字列信号の種類
  wParamに1が入っていたら文字列の初期化信号
  ということにする
*/

4. 一文字ずる信号を送ります

一文字ずる送るの例.cpp
for(int i = 0;i < strlen(message);i++){
    SendMessage(hWnd, 0x0EDA, 2, message[i]);
    /* wParamが2だったらlParamの数字を
       文字として受信側が変数に追加する */
}

5. 終了処理をします
初期化と合わせることで必要なくなることもできます

終了処理の例.cpp
SendMessage(hWnd, 0x0EDA, 3, 0);
//wParamが3だったら終了処理

受信側

WndProcに色々追加します。

WndProcの色々.cpp
string text;
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
    //適当です。
    switch(msg){
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0; break;
    case 0x0EDA: //文字列転送のmsg
        switch(wp){
            case 1: //初期化
                text = new string();
                break;
            case 2: //文字列の追加
                text.append((char)lp);
                break;
            case 3: //終了処理
                MessageBoxA(NULL, text, "テキストの転送", MB_OK);
                //今回はメッセージボックスにする
                break;
        }
        return 0; break;
    }
}

wParamとかlParamに文字列ポインタを入れようとしても上手くいかなかったですが、これならうまくいくと思うので書きました。ぜひ参考にしてほしいです。

1
0
3

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
1
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?