Quantcast
Channel: 楽天 みんなで解決!Q&A 新着質問([技術者向] コンピューター/205)
Viewing all articles
Browse latest Browse all 29232

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

$
0
0
現在、VC++でアプリケーションを作っています。 超初心者なのでわからない事だらけです。 どなたかご教授お願いします。 <質問内容> sendmessageで送信した文字列をtextboxに表示したら文字化けします。 原因がわからないので、もし何か気づかれた方がおられましたら教えてください。 特にどのように変更すれば良いかご指摘いただけると本当に助かります。 <開発環境> Windows XPモード Visual Studio 2010 professional Visual C++ windows フォームアプリケーション <アプリ動作> 2つのアプリケーションを使用し、片方のアプリから文字列を送り、もう片方のアプリで受信します。 また、受け取った側のアプリケーションは受け取った文字列をtextboxに表示します。 送信側はsendmessage関数で文字列を送信し、受信側はwndproc関数と、共有メモリを使用して受信と表示を行っています。 下記がソースです。 ~送信側ソース~ #pragma once #include #include #include #include #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 #include #include 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_ptrpstr=PtrToStringChars(str); System::String^ ShareMemoryName1=L"LotInformation"; HANDLE hmap; LPVOID pmap; marshal_context^ context= gcnew marshal_context; LPCTSTR ShareMemoryName2 = context->marshal_as(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); }

Viewing all articles
Browse latest Browse all 29232

Trending Articles