VC++ 2010で文字化けする問題について

このQ&Aのポイント
  • VC++ 2010でsendmessage関数を使用して文字列を送信すると、textboxに表示する際に文字化けする問題が発生しています。
  • sendmessage関数を使用して文字列を送信した後、受信側のアプリケーションで受け取った文字列が正常に表示されません。
  • 問題の原因がわからず、解決策を探しています。VC++に詳しい方がいらっしゃいましたら、教えていただけると助かります。
回答を見る
  • ベストアンサー

VC++ 2010 メッセージが文字化けします。

現在、VC++でアプリケーションを作っています。 超初心者なのでわからない事だらけです。 どなたかご教授お願いします。 <質問内容> sendmessageで送信した文字列をtextboxに表示したら文字化けします。 原因がわからないので、もし何か気づかれた方がおられましたら教えてください。 特にどのように変更すれば良いかご指摘いただけると本当に助かります。 <開発環境> Windows XPモード Visual Studio 2010 professional Visual C++ windows フォームアプリケーション <アプリ動作> 2つのアプリケーションを使用し、片方のアプリから文字列を送り、もう片方のアプリで受信します。 また、受け取った側のアプリケーションは受け取った文字列をtextboxに表示します。 送信側はsendmessage関数で文字列を送信し、受信側はwndproc関数と、共有メモリを使用して受信と表示を行っています。 下記がソースです。 ~送信側ソース~ #pragma once #include<windows.h> #include<iostream> #include<fstream> #include<string> #pragma comment(lib,"user32.lib") using namespace std; using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Runtime::InteropServices; [DllImport("user32.dll") ] extern System::String^ FindWindow(String^ lpClassName, String^ lpWindowName); [DllImport("user32.dll")] extern System::String^ SendMessage(HWND hWnd, int Msg, int wParam, int lParam); public: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {              COPYDATASTRUCT cd; HWND hWnd; char buffer[500]; strcpy_s(buffer,"失敗しました。");       cd.dwData=0; cd.cbData=strlen(buffer)+1; cd.lpData=buffer; hWnd=::FindWindow(nullptr,L"ソフト"); ::SendMessage((HWND)hWnd,WM_COPYDATA,0,(LPARAM)&cd); } ~受信側ソース~ #pragma once #pragma comment(lib,"user32.lib") #include<ctype.h> #include<windows.h> #include<msclr/marshal.h> using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Runtime::InteropServices; using namespace msclr::interop; public: virtual void WndProc(System::Windows::Forms::Message% msg) override { if(msg.Msg== WM_COPYDATA) { COPYDATASTRUCT *cd; cd=(COPYDATASTRUCT *)msg.LParam.ToInt32(); System::String^ str; str=gcnew System::String((wchar_t*)cd->lpData); pin_ptr<const wchar_t>pstr=PtrToStringChars(str); System::String^ ShareMemoryName1=L"LotInformation"; HANDLE hmap; LPVOID pmap; marshal_context^ context= gcnew marshal_context; LPCTSTR ShareMemoryName2 = context->marshal_as<LPCTSTR>(ShareMemoryName1); hmap=OpenFileMapping(FILE_MAP_WRITE,FALSE,ShareMemoryName2); if(hmap==NULL) { hmap=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,(DWORD)0,(DWORD)2048,(LPCTSTR)ShareMemoryName2); if(hmap!=NULL) { pmap=MapViewOfFile(hmap,FILE_MAP_WRITE,0,0,0); hmap=OpenFileMapping(FILE_MAP_WRITE,FALSE,ShareMemoryName2); if(pmap!=NULL) { ZeroMemory(pmap,2048); } } } else { pmap=MapViewOfFileEx(hmap,FILE_MAP_WRITE,0,0,0,NULL); } ZeroMemory(pmap,2048); memcpy_s(pmap,2048,pstr,sizeof(pstr)); System::String^ str1; str1= gcnew System::String((const wchar_t *)pmap); textBox5->Text=str1; UnmapViewOfFile(pmap); CloseHandle(hmap); } Form::WndProc(msg); }

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

送信側がマルチバイト文字列(char*)を渡しているのに、受信側がワイド文字列のポインタ(wchar_t*)として受けているためです。送信側と受信側で一致させる必要があります。 …で、それを一致させれば、受信側の最初の str で文字列を取れているはずですが(プロセス間通信に必要な共有メモリでのやり取りは WM_COPYDATA メッセージがすべて代行してくれるので、他にやることは何もない)、それ以降に記載されているマッピングオブジェクトの作成云々は、ここでの送信側でも受信側でもない第三のプログラムが見るために共有メモリに格納しているということでしょうか? 少なくとも、ここの送信側と受信側のやり取りには全く意味のないものですが。

rafute
質問者

お礼

->fresh_homepieさん 回答ありがとうございます。 無事文字列が表示されました。 共有メモリを使用しているのは、おっしゃるとおり第三のプログラムが介入するためです。 ただし、これは前任者からの指示でやっているので自分はよくわかっていません。 初心者ですし、全部はすぐに理解できませんので。 また、今のプログラムはメッセージのやり取りを確認するために固定文字列の送受信を試しているプログラムですので、今後は複数のアプリケーションから変動型の文字列をメッセージとして送受信します。 外部からの変動型文字列をメッセージで送受信するためにも共有メモリを使用しているのだと思います。 本当にありがとうございました。 またわからないことがあれば質問させていただきますのでよろしくお願いします。

関連するQ&A

  • VC++メッセージの送受信について教えてください。

    VC++でソフトを作成しています。 初心者なのでわからないことだらけです。 どなたかご教授お願いします。 ■環境 Windows xp mode Visual Studio 2010 Professional VC++ フォームアプリケーション .net Framework4.0 ■相談内容 アプリ1のtextBoxに入力された文字列をアプリ2に送信して、アプリ2のtextBoxに表示させたいのですが、PostMessageを使用するとメッセージが送れません。 また、SendMessageを使用すると送れますが、共有メモリを使用すると文字列が途中で途切れてしまいます。 PostMessageと共有メモリの使用は指令なのではずせません。 理由は送信側のアプリがロックされるのを防ぐため、後に多数のアプリから送信した文字列を取得できるようにするためです。 下記にソースコードを記載しますので、どこが悪いのか、何が原因でそうなるのか、どうすれば正常に動作するようにできるのかを教えてください。 特に、ソースについてはどこをどのように直せば良いかを教えていただけるとありがたいです。 ~送信側ソース~ #pragma once #include<windows.h> #include<iostream> #include<fstream> #include<string> #include<msclr/marshal.h> #pragma comment(lib,"user32.lib") int s; using namespace std; using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Runtime::InteropServices; using namespace msclr::interop; [DllImport("user32.dll") ] extern System::String^ FindWindow(String^ lpClassName, String^ lpWindowName); [DllImport("user32.dll")] extern System::String^ PostMessage(HWND hWnd, int Msg, int wParam, int lParam); public: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { System::String^ moji_textBox4; moji_textBox4=textBox4->Text; s=textBox4->Text->Length+1; COPYDATASTRUCT cd; HWND hWnd; char buffer[500]; sprintf_s(&buffer[0],5,"%s",moji_textBox4); cd.dwData=0; cd.cbData=s;//strlen(buffer)+1; cd.lpData=buffer; hWnd=::FindWindow(nullptr,L"アプリ2"); ::PostMessage((HWND)hWnd,WM_COPYDATA,0,(LPARAM)&cd); ~受信側ソース~ #pragma once #pragma comment(lib,"user32.lib") #include<ctype.h> #include<windows.h> #include<msclr/marshal.h> using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Runtime::InteropServices; using namespace msclr::interop; public: virtual void WndProc(System::Windows::Forms::Message% msg) override { if(msg.Msg== WM_COPYDATA) { COPYDATASTRUCT *cd; cd=(COPYDATASTRUCT *)msg.LParam.ToInt32(); System::String^ str; str=gcnew System::String((char *)cd->lpData); pin_ptr<const wchar_t>pstr=PtrToStringChars(str); System::String^ ShareMemoryName1=L"Information"; HANDLE hmap; char *pmap; marshal_context^ context= gcnew marshal_context; LPCTSTR ShareMemoryName2 = context->marshal_as<LPCTSTR>(ShareMemoryName1); hmap=::CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,2048, (LPCTSTR)ShareMemoryName2); pmap=(char *)::MapViewOfFile(hmap,FILE_MAP_ALL_ACCESS,0,0,1024); System::String^ pstr1= gcnew System::String(pstr); ZeroMemory(pmap,2048); memcpy_s(pmap,2048,pstr,sizeof(pstr)); System::String^ str1; str1= gcnew System::String((char *)pmap); textBox6->Text=str1; UnmapViewOfFile(pmap); CloseHandle(hmap); } Form::WndProc(msg); }

  • System::String->char*変換でき

    C++/CLIでSystem::String^文字列をchar*に変換する関数を書いたのですが、256文字以上の文字列を投げて、戻値を確認してみると、255文字分しか中身が詰まっていません。元の文字列はアスキーのみです。 #include <msclr/marshal.h> using namespace msclr::interop; static char* toPtChar(const String^ Text) { String^ temp = (String^)Text; msclr::interop::marshal_context^ context = gcnew msclr::interop::marshal_context(); char* res = ((char*)(context->marshal_as<const char*>(temp))); return res; } この関数をどのように変更すれば、256文字以上変換できるでしょうか?

  • VC++でSELECT文の実行結果を表示

    開発環境は OS XPのHomeEditon Visual C++ ExpressEdition SQL Server 2005 Express です。 よろしくお願いします。 #include "stdafx.h" #include <stdio.h> #include <windows.h> #include <string> using namespace System; using namespace System::Data; using namespace System::Data::SqlClient; char a; void sub( SqlDataReader ^objRd ) { System::Diagnostics::Debug::WriteLine(objRd); while( objRd->Read() ) { System::Diagnostics::Debug::WriteLine(objRd->Read()); } } int main(void){ String^ str; SqlConnection^ sqlConn = gcnew SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\SOUTUKEN\\TEST.mdf;Integrated Security=True;User Instance=True"); sqlConn->Open(); str = "SELECT * FROM table1"; SqlCommand^ sqlCmd = gcnew SqlCommand(str,sqlConn); SqlDataReader^ exeReader = sqlCmd->ExecuteReader(); sub( exeReader ); sqlConn->Close(); } これの実行結果がboolean形で帰ってくるのですがこれを表内のデータが帰ってくるようにしたいのです。 少し知識不足なところもありますがどうかよろしくお願いします。

  • VC2010プログラミング

    formの中に自作のクラスを定義して使用したいのですが、どこに定義すればよいのかわかりません。 #pragma once #include "XXX.h" namespace FileRename { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Form1 の概要 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクター コードを追加します // } protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; XXX^ instance = gcnew XXX(); protected: private: /// <summary> /// 必要なデザイナー変数です。 /// </summary> System::ComponentModel::Container ^components;   ・   ・   ・ } XXXクラスはこの場所で定義するのは間違いなのでしょうか?

  • デバッグ中の変数参照について

    VS2005のC++/CLIでアプリを作っているものです。 デバッグ中に作成したメソッドの引数に渡されて来る値をウォッチで 見ようとすると「エラー: 識別子 'str' はスコープ外です」となり 中身が見れません。 作成したクラスのメンバ変数は見れます。 以下が発生したソースです。 ---------------------------- TestClass.cpp ---------------------------- #include "StdAfx.h" #include "TestClass.h" using namespace System; TestClass::TestClass(void) { } // 追加メソッド void TestClass::func( String^ str ){ String^ tmp; tmp = str; } ---------------------------- TestClass.h ---------------------------- #pragma once using namespace System; ref class TestClass { public: TestClass(void); // 追加メソッド void func( String^ ); }; ---------------------------- Form1.h ---------------------------- #pragma once #include "TestClass.h" namespace test2 { (省略) public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクタ コードを追加します // TestClass^ aaa = gcnew TestClass(); aaa->func( "あいうえお" ); } (省略) ---------------------------- プロジェクトは、CLRのWindowsフォームアプリケーションで作成しています。 初心者の質問ですが、宜しくお願い致します。

  • C++/CLI 関数がインポートできない

    Visual C++でビルドすると、 1>ConnectionWrapsTest.cpp(12): warning C4679: 'ConnectionWraps::Class1::GetConnection' : メンバーをインポートできませんでした 1> 型 'ConnectionWraps::Class1 ' をアセンブリ 'ConnectionWraps, Version=1.0.4594.40998, Culture=neutral, PublicKeyToken=null' からインポート中に、この診断が発生しました。 という様なエラーがいくつかでて、失敗してしまいます。 何故この様なエラーが出るのか理解出来ません。解決策を教えてください。 DLL側 ------------------------------------------- // ConnectionWraps.h #pragma once using namespace System; using namespace std; #include <string> namespace ConnectionWraps { public ref class Class1 { // TODO: このクラスの、ユーザーのメソッドをここに追加してください。 public: static int GetConnection(string address, string port, string database, string userid, string password); static void CloseConnection(int handle); static int ChangeDatabase(int handle, string databaseName); static int Command(int handle, string sql); }; } -------------------------------------------------- 実行ファイル側 ---------------------------------------------------- // ConnectionWrapsTest.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" #include <string> using namespace std; using namespace System; using namespace ConnectionWraps; int main(array<System::String ^> ^args) { Console::WriteLine(Class1::GetConnection("a", "a", "a", "a", "a", "a")); return 0; } -------------------------------------------------

  • 文字化けを直したい

    visual stadio C++ で、テキストファイルを読み込み、それをウインドウに表示するというプログラムを作っているのですが、表示すると文字化けしてしまいます。 原因がわかりません。 教えてください。 VC++ 2010です。 サイトを見て設定でマルチバイトに変えてあります case IDB_SELECT:{ HDC hDC; errno_t error; static LPCTSTR TEST_STR ;//= _T("左ボタンが押されました"); // ファイルを開くコモンダイアログを作成 if( !GetOpenFileName( &ofn ) ){ MessageBox( hWnd, _T("エラー"), _T("エラー"), MB_OK ); SendMessage( hWnd, WM_CLOSE, 0, 0 ); return 0; } // 選択されたファイル名を表示 MessageBox( hWnd, filename_full, _T("OK"), MB_OK ); int k=0; fp = fopen( "test.txt", "r" ); if( fp == NULL ) /* 関数が失敗していないか */ { MessageBox( hWnd,"読み込み失敗", _T("bad"), MB_OK ); return 1; /* 異常終了は0以外を返す */ } MessageBox( hWnd,"読み込み成功", _T("good"), MB_OK ); hDC = GetDC( hWnd ); while (fgets(file, 80, fp) != NULL){ TEST_STR = &file[k]; TextOut( hDC, 50, k*15+50, TEST_STR, (int)_tcslen(TEST_STR) ); k++; } fclose(fp); } return 0L; } InvalidateRect(hWnd, NULL, TRUE); // 再描画命令発 return 0L;

  • VC++でSQLへSELECT文を送ったのですが…

    #include "stdafx.h" #include <stdio.h> #include <windows.h> #include <string> using namespace System; using namespace System::Data; using namespace System::Data::SqlClient; char a; int main(void) { String^ str; SqlConnection^ sqlConn = gcnew SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\○○\\test.mdf;Integrated Security=True;User Instance=True"); sqlConn->Open(); str = "Select test FROM Table1"; SqlCommand^ sqlCmd = gcnew SqlCommand(str,sqlConn);// SqlDataReader^ ExecuteReader (); sqlConn->Close(); } int sub() { a = ExecuteReader; printf (a,"表示テスト\n"); return 0; } の構文でSQL ServerへSELECT文を送り、そこで得た結果を 表示しようと思ったのですが 1>select-test.cpp 1>.\select-test.cpp(28) : error C2065: 'ExecuteReader' : 定義されていない識別子です。 1>.\select-test.cpp(29) : error C2664: 'printf' : 1 番目の引数を 'char' から 'const char *' に変換できません。 (新しい機能 ; ヘルプを参照) 1> 整数型からポインタ型への変換には reinterpret_cast、C スタイル キャストまたは 関数スタイル キャストが必要です。 と2つのエラーを返されてしまいました。 開発環境は OS XPのHomeEditon Visual C++ ExpressEdition SQL Server 2005 Express です。 よろしくお願いします。

  • C++

    C++で書いた下記のプログラムが、文字の入力が1023個までなら実行されますが、1024個を超えると実行されません。なぜだか分かる方、教えてください。 #include <iostream> #include <string> using namespace std; int main(void) { string Str; cin >> Str; cout << "here"; }

  • io error flag を消去したいのですが

    下記プログラムで一度入力がフェイルすると正常に入力動作ができなくなります フラグをリセットする方法を教えてください #include <string> #include <iostream> #include <fstream> using namespace std; void main(void) { char buffer[256]; string str; char c; ifstream ifs; //gomi.txt="123" ifs.open("gomi.txt"); ifs.read(buffer,8); //errorフラグをリセットするためにここに何がいるか教えてください ifs.iostate=0; ifs.seekg(0); for(str="";true;str+=c){ifs.get(c);if(ifs.eof()!=0)break;} cout<<"gomi.txt:"<<str<<endl; }

専門家に質問してみよう