gpt4 book ai didi

c# - ReadMsgQueue 返回 ERROR_INVALID_PARAMETER

转载 作者:可可西里 更新时间:2023-11-01 10:06:28 29 4
gpt4 key购买 nike

Windows 嵌入式紧凑型。

Trying to send and receive a message with MsgQueue.

看起来写入工作正常但读取给出了不正确的参数错误。

知道缺少什么吗?

我不确定创建队列函数。它说必须调用两次才能获得读取或写入句柄 - 对吗?

    int main()
{



MSGQUEUEOPTIONS options = {0};

options.dwSize = sizeof(options); //
options.dwFlags = MSGQUEUE_NOPRECOMMIT;
options.dwMaxMessages = 10000; // msg max for queue
options.cbMaxMessage = 50; // max number of bytes in each msg





options.bReadAccess = TRUE; // read
HANDLE hRead = CreateMsgQueue(LPCWSTR("MSG_QUEUE"), &options);
if ( hRead == NULL )
{
printf("CreateMsgQueue hRead failed! Err code: %d\n", GetLastError());
}

// Thread A will read msg queue
HANDLE hTg = CreateThread(NULL, 0, threadA, hRead, 0, 0);
if ( NULL == hTg )
{
printf("CreateThread failed - A!\n");
return 1;
}

Sleep(1000); //give time before sending msg


options.bReadAccess = FALSE; // write to
HANDLE hWrte = CreateMsgQueue(LPCWSTR("MSG_QUEUE"), &options);
if ( hWrte == NULL )
{
printf("CreateMsgQueue hWrte failed! Err code: %d\n", GetLastError());
}

// Thread B write to queue
HANDLE hTt = CreateThread(NULL, 0, threadB, hWrte, 0, 0);
if ( NULL == hTt )
{
printf("CreateThread failed - B!\n");
return 1;
}
// quit on <ENTER> key
getchar();

}

DWORD WINAPI threadB(LPVOID lpParameter)
{
HANDLE msgH = HANDLE(lpParameter);
if ( msgH == NULL)
{
printf("Null handle in write!\n");
}

char message[10] = "ABCDEFGHI";

printf("Size of message sent: %d bytes\n", sizeof(message));


// know that a queue is not full and that it's safe to write
WaitForSingleObject(msgH, INFINITE);

BOOL ret = WriteMsgQueue(
msgH,
&message,
sizeof(message),
INFINITE,
NULL);
if ( ret == FALSE )
{
printf("WriteMsgQueue failed! Err code: %d\n", GetLastError());
}

return 0;
}

DWORD WINAPI threadA(LPVOID lpParameter)
{
HANDLE hQ = HANDLE(lpParameter);
if ( hQ == NULL )
{
printf("null handle in read!\n");
}

char readIn[50] = {0};
LPDWORD numRead = 0;
//DWORD flag;

// need to wait on sinfle object
WaitForSingleObject(hQ, INFINITE);

BOOL ret = ReadMsgQueue(
hQ,
&readIn,
DWORD(sizeof(readIn)),
numRead,
INFINITE,
NULL
);

if ( ret == FALSE )
{
printf(" ReadMsgQueue failed! Err code: %d\n", GetLastError());
}

printf("Size received: %d\n", numRead);
printf("Msg Received: %s\n", readIn);


return 0;
}

编辑

这是在@WhozCraig 之后的工作读取函数和发现的另一个问题,以防其他人需要它:

DWORD WINAPI threadA(LPVOID lpParameter)
{
HANDLE hQ = HANDLE(lpParameter);
if ( hQ == NULL )
{
printf("null handle in read!\n");
}

char readIn[50] = {0};
DWORD numRead = 0;
DWORD flag;

// need to wait on sinfle object
WaitForSingleObject(hQ, INFINITE);

BOOL ret = ReadMsgQueue(
hQ,
&readIn,
sizeof(readIn),
&numRead,
INFINITE,
&flag
);

if ( ret == FALSE )
{
printf(" ReadMsgQueue failed! Err code: %d\n", GetLastError());
}

printf("Size received: %d\n", numRead);
printf("Msg Received: %s\n", readIn);

return 0;
}

最佳答案

是的,这就是您为发送/接收设置消息队列的方式。一般采用二开。

我相信你的问题ReadMsgQueue() invocation 是接收到的大小参数。它是一个 LPDWORD,不能为 NULL。你目前正在这样做:

char readIn[50] = {0};
LPDWORD numRead = 0; // SHOULD NOT BE A POINTER

// need to wait on sinfle object
WaitForSingleObject(hQ, INFINITE);

BOOL ret = ReadMsgQueue(
hQ,
&readIn,
DWORD(sizeof(readIn)),
numRead, // THIS IS A PROBLEM
INFINITE,
NULL
);

您正在传递 NULL。您需要这样做:

char readIn[50] = {0};
DWORD numRead = 0; // NOTE: regular DWORD

// need to wait on sinfle object
WaitForSingleObject(hQ, INFINITE);

BOOL ret = ReadMsgQueue(
hQ,
&readIn,
DWORD(sizeof(readIn)),
&numRead, // NOTE: address of DWORD
INFINITE,
NULL
);

我不清楚最后一个参数,received message flags out-paramter 是否可以为 NULL。它没有像读取大小的 DWORD 地址那样在文档中被特别调用,它明确指出 This parameter cannot be NULL,其他参数也是如此。所以你的里程可能会有所不同。

关于c# - ReadMsgQueue 返回 ERROR_INVALID_PARAMETER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20026796/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com