首先感謝大家打開小妹的帖子!初學MFC,要實作win32控制臺程式和MFC之間利用命名管道的行程間通信,說白就是傳輸資料==
糾結了十幾天,總算有點眉目,卻還是沒有得出想要的結果。
首先win32程式中讀出一個二進制檔案的資料,保存在陣列里,之后需要通過管道傳遞給MFC。
MFC 里面我自定義了一個訊息WM_PIPE,以及一個訊息函式LOADDATA,這部分的代碼如下:
void CMFCPIPEINDlg::LOADDATA(WPARAM wparam, LPARAM lparam)
{
HANDLE hNamedPipe = CreateNamedPipe(
TEXT("\\\\.\\pipe\\mypipe"),//name of the pipe
PIPE_ACCESS_DUPLEX,//1-way pipe, server can only receive
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,//data as byte streams
2, //allows two instances
0,
0,
NMPWAIT_WAIT_FOREVER,//always wait
NULL);//use default security attributes
if (INVALID_HANDLE_VALUE == hNamedPipe){ AfxMessageBox(TEXT("CreateNamedPipe Failed.")); return; }
else AfxMessageBox(TEXT("CreateNamedPipe Success."));
AfxMessageBox(TEXT("Waiting for the clinet to connect to the pipe..."));
if (!ConnectNamedPipe(hNamedPipe, NULL)){
MessageBox(TEXT("Cannot connect Named Pipe"));
CloseHandle(hNamedPipe); return;
}else AfxMessageBox(TEXT(" Connect Named Pipe"));
short *pReadBuf = new short;
memset(pReadBuf, 0, sizeof(short));
DWORD dwRead = 0;
if (!ReadFile(hNamedPipe, pReadBuf, sizeof(short), &dwRead, NULL)){
if (NULL != pReadBuf){ delete(pReadBuf); pReadBuf = NULL; }
AfxMessageBox(TEXT("Read file failed!"));
}
DisconnectNamedPipe(hNamedPipe);
CloseHandle(hNamedPipe);}
void CMFCPIPEINDlg::OnBnClickedButton1()
{
// TODO: 在此添加控制元件通知處理程式代碼
this->PostMessageW(WM_PIPE, 0, 0);
}
控制臺的代碼如下:
#define _CRT_SECURE_NO_WARNINGS
#define CHUNK 1024
#include<stdio.h>
#include<afxwin.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
CString strPipeName = TEXT("\\\\.\\pipe\\mypipe");
cout << "Waiting for connecting Pipe..." << endl;
if (!WaitNamedPipe(strPipeName, NMPWAIT_WAIT_FOREVER)){
cout << "Named pipe not exist!" << endl; return 0;
}
else cout << "Named pipe exist!" << endl;
HANDLE hNamedPipe = CreateFile(strPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hNamedPipe)
{
cout << "Create Pipe Failed!" << GetLastError() << endl; return 0;
}
else cout << "Create Pipe Success!" << GetLastError() << endl;
FILE *fp;
fp = fopen("C4.ebm", "rb"); //open file and check error
if (fp == NULL)
{
printf("error!");
exit(0);
}
// calculate file size
int prev = ftell(fp);
fseek(fp, 0L, SEEK_END);
int sz = ftell(fp);
fseek(fp, prev, SEEK_SET);
//printf("%d\n", sz);
//load file(int16=short)
unsigned short *array;
array = (unsigned short *)malloc(sizeof(unsigned short)*sz / 2);
int res = fread(array, sizeof(unsigned short), sz / 2, fp);
fclose(fp);
//printf("%d\n", res);
for (int i = 0; i < 1000; i++)
{
if (array[i] >= 32768)
{
array[i] = 65536 - array[i];
//printf("-%u\n", array[i]);
}
//else
// printf("%u\n", array[i]);
}
DWORD dwWRITE = 0;
if (!WriteFile(hNamedPipe, &array, sizeof(short), &dwWRITE, NULL))
{cout << "Read data failed!" << endl;}
cout << "Read data success!" << endl;
CloseHandle(hNamedPipe);
free(array);
system("pause");
return(0);
}
首先運行MFC到這個界面

確認后運行控制臺程式得到下圖==

接著MFC就會顯示出管道建立成功

接著該傳資料時==出了個這個框框,不明所以/(ㄒoㄒ)/~~

大神們能幫我支個招不==我實在是沒辦法了
再次感謝大大們閱讀完此題!小妹感激不盡!
另外有一個程式可以參考參考:http://avidinsight.uk/2012/03/introduction-to-win32-named-pipes-cpp/#more-139
謝謝大家了!
uj5u.com熱心網友回復:
for (int i = 0; i < 1000; i++) for (int i = 0; i < sz; i++)注意越界專案屬性中Link方式選擇 __cdecl
uj5u.com熱心網友回復:
能改用socket傳輸嗎?uj5u.com熱心網友回復:
點已下中斷,看下堆疊呼叫視窗里API的呼叫關系uj5u.com熱心網友回復:
崩潰的時候在彈出的對話框按相應按鈕進入除錯,按Alt+7鍵查看Call Stack即“呼叫堆疊”里面從上到下列出的對應從里層到外層的函式呼叫歷史。雙擊某一行可將游標定位到此次呼叫的源代碼或匯編指令處,看不懂時雙擊下一行,直到能看懂為止。uj5u.com熱心網友回復:
錯誤是“函式約定”不對uj5u.com熱心網友回復:
資料堆疊溢位等。陣列越界等,基本上就是這幾個地方uj5u.com熱心網友回復:
http:////http://www.codeproject.com/Articles/5531/Redirecting-an-arbitrary-Console-s-Input-Outputuj5u.com熱心網友回復:
//http://www.codeproject.com/Articles/5531/Redirecting-an-arbitrary-Console-s-Input-Outputuj5u.com熱心網友回復:
http://www.codeproject.com/Articles/5531/Redirecting-an-arbitrary-Console-s-Input-Output轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/131302.html
標籤:進程/線程/DLL
