Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

C++0x and windows dll

vetroxl
Beginner
519 Views
Hi, has anyone comeacrossany problems using the C++0x features in a dll. I getweirderrorslike "Offset is not a member of _OVERLAPPED"?`
0 Kudos
6 Replies
Brandon_H_Intel
Employee
519 Views
Hi vetroxl,

What version of Visual Studio* are you using, and what C++0x features in particular are you using? I took an example from MSDN that uses Overlapped I/O, but I don't get any compiler errors with C++0x enabled on Visual Studio 2008.
0 Kudos
vetroxl
Beginner
519 Views
Hi I;m using Visual Studio 2008. I'm trying to use the lambda expressions. However, I just have to turn on the C++0x support and I get the errors. I'm also compiling an app using the WTL
0 Kudos
vetroxl
Beginner
519 Views
[cpp]typedef struct _OVERLAPPED {
    ULONG_PTR Internal;
    ULONG_PTR InternalHigh;
    union {
        struct {
            DWORD Offset;
            DWORD OffsetHigh;
        } DUMMYSTRUCTNAME;
        PVOID Pointer;
    } DUMMYUNIONNAME;

    HANDLE  hEvent;
} OVERLAPPED, *LPOVERLAPPED;
[/cpp]
It looks like it's a problem with anonymous struct in OVERLAPPED definition
0 Kudos
Brandon_H_Intel
Employee
519 Views
That looks like what I'm using except that it's missing the DUMMYSTRUCTNAME and DUMMYUNIONNAME names. Did you insert those yourself?

Here is my code (I got this from http://msdn.microsoft.com/en-us/library/aa365603%28v=VS.85%29.aspx and then tweaked it to remove the tmain part so I could compile as a dll) which compiles without error.

[cpp]#include  
#include
#include
#include

#define CONNECTING_STATE 0
#define READING_STATE 1
#define WRITING_STATE 2
#define INSTANCES 4
#define PIPE_TIMEOUT 5000
#define BUFSIZE 4096

typedef struct
{
OVERLAPPED oOverlap;
HANDLE hPipeInst;
TCHAR chRequest[BUFSIZE];
DWORD cbRead;
TCHAR chReply[BUFSIZE];
DWORD cbToWrite;
DWORD dwState;
BOOL fPendingIO;
} PIPEINST, *LPPIPEINST;


VOID DisconnectAndReconnect(DWORD);
BOOL ConnectToNewClient(HANDLE, LPOVERLAPPED);
VOID GetAnswerToRequest(LPPIPEINST);

PIPEINST Pipe[INSTANCES];
HANDLE hEvents[INSTANCES];

__declspec(dllexport) int foo()//int _tmain(VOID)
{
DWORD i, dwWait, cbRet, dwErr;
BOOL fSuccess;
LPTSTR lpszPipename = TEXT("\\.\pipe\mynamedpipe");

// The initial loop creates several instances of a named pipe
// along with an event object for each instance. An
// overlapped ConnectNamedPipe operation is started for
// each instance.

for (i = 0; i < INSTANCES; i++)
{

// Create an event object for this instance.

hEvents = CreateEvent(
NULL, // default security attribute
TRUE, // manual-reset event
TRUE, // initial state = signaled
NULL); // unnamed event object

if (hEvents == NULL)
{
printf("CreateEvent failed with %d.n", GetLastError());
return 0;
}

Pipe.oOverlap.hEvent = hEvents;

Pipe.hPipeInst = CreateNamedPipe(
lpszPipename, // pipe name
PIPE_ACCESS_DUPLEX | // read/write access
FILE_FLAG_OVERLAPPED, // overlapped mode
PIPE_TYPE_MESSAGE | // message-type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
INSTANCES, // number of instances
BUFSIZE*sizeof(TCHAR), // output buffer size
BUFSIZE*sizeof(TCHAR), // input buffer size
PIPE_TIMEOUT, // client time-out
NULL); // default security attributes

if (Pipe.hPipeInst == INVALID_HANDLE_VALUE)
{
printf("CreateNamedPipe failed with %d.n", GetLastError());
return 0;
}

// Call the subroutine to connect to the new client

Pipe.fPendingIO = ConnectToNewClient(
Pipe.hPipeInst,
&Pipe.oOverlap);

Pipe.dwState = Pipe.fPendingIO ?
CONNECTING_STATE : // still connecting
READING_STATE; // ready to read
}

while (1)
{
// Wait for the event object to be signaled, indicating
// completion of an overlapped read, write, or
// connect operation.

dwWait = WaitForMultipleObjects(
INSTANCES, // number of event objects
hEvents, // array of event objects
FALSE, // does not wait for all
INFINITE); // waits indefinitely

// dwWait shows which pipe completed the operation.

i = dwWait - WAIT_OBJECT_0; // determines which pipe
if (i < 0 || i > (INSTANCES - 1))
{
printf("Index out of range.n");
return 0;
}

// Get the result if the operation was pending.

if (Pipe.fPendingIO)
{
fSuccess = GetOverlappedResult(
Pipe.hPipeInst, // handle to pipe
&Pipe.oOverlap, // OVERLAPPED structure
&cbRet, // bytes transferred
FALSE); // do not wait

switch (Pipe.dwState)
{
// Pending connect operation
case CONNECTING_STATE:
if (! fSuccess)
{
printf("Error %d.n", GetLastError());
return 0;
}
Pipe.dwState = READING_STATE;
break;

// Pending read operation
case READING_STATE:
if (! fSuccess || cbRet == 0)
{
DisconnectAndReconnect(i);
continue;
}
Pipe.cbRead = cbRet;
Pipe.dwState = WRITING_STATE;
break;

// Pending write operation
case WRITING_STATE:
if (! fSuccess || cbRet != Pipe.cbToWrite)
{
DisconnectAndReconnect(i);
continue;
}
Pipe.dwState = READING_STATE;
break;

default:
{
printf("Invalid pipe state.n");
return 0;
}
}
}

// The pipe state determines which operation to do next.

switch (Pipe.dwState)
{
// READING_STATE:
// The pipe instance is connected to the client
// and is ready to read a request from the client.

case READING_STATE:
fSuccess = ReadFile(
Pipe.hPipeInst,
Pipe.chRequest,
BUFSIZE*sizeof(TCHAR),
&Pipe.cbRead,
&Pipe.oOverlap);

// The read operation completed successfully.

if (fSuccess && Pipe.cbRead != 0)
{
Pipe.fPendingIO = FALSE;
Pipe.dwState = WRITING_STATE;
continue;
}

// The read operation is still pending.

dwErr = GetLastError();
if (! fSuccess && (dwErr == ERROR_IO_PENDING))
{
Pipe.fPendingIO = TRUE;
continue;
}

// An error occurred; disconnect from the client.

DisconnectAndReconnect(i);
break;

// WRITING_STATE:
// The request was successfully read from the client.
// Get the reply data and write it to the client.

case WRITING_STATE:
GetAnswerToRequest(&Pipe);

fSuccess = WriteFile(
Pipe.hPipeInst,
Pipe.chReply,
Pipe.cbToWrite,
&cbRet,
&Pipe.oOverlap);

// The write operation completed successfully.

if (fSuccess && cbRet == Pipe.cbToWrite)
{
Pipe.fPendingIO = FALSE;
Pipe.dwState = READING_STATE;
continue;
}

// The write operation is still pending.

dwErr = GetLastError();
if (! fSuccess && (dwErr == ERROR_IO_PENDING))
{
Pipe.fPendingIO = TRUE;
continue;
}

// An error occurred; disconnect from the client.

DisconnectAndReconnect(i);
break;

default:
{
printf("Invalid pipe state.n");
return 0;
}
}
}

return 0;
}


// DisconnectAndReconnect(DWORD)
// This function is called when an error occurs or when the client
// closes its handle to the pipe. Disconnect from this client, then
// call ConnectNamedPipe to wait for another client to connect.

VOID DisconnectAndReconnect(DWORD i)
{
// Disconnect the pipe instance.

if (! DisconnectNamedPipe(Pipe.hPipeInst) )
{
printf("DisconnectNamedPipe failed with %d.n", GetLastError());
}

// Call a subroutine to connect to the new client.

Pipe.fPendingIO = ConnectToNewClient(
Pipe.hPipeInst,
&Pipe.oOverlap);

Pipe.dwState = Pipe.fPendingIO ?
CONNECTING_STATE : // still connecting
READING_STATE; // ready to read
}

// ConnectToNewClient(HANDLE, LPOVERLAPPED)
// This function is called to start an overlapped connect operation.
// It returns TRUE if an operation is pending or FALSE if the
// connection has been completed.

BOOL ConnectToNewClient(HANDLE hPipe, LPOVERLAPPED lpo)
{
BOOL fConnected, fPendingIO = FALSE;

// Start an overlapped connection for this pipe instance.
fConnected = ConnectNamedPipe(hPipe, lpo);

// Overlapped ConnectNamedPipe should return zero.
if (fConnected)
{
printf("ConnectNamedPipe failed with %d.n", GetLastError());
return 0;
}

switch (GetLastError())
{
// The overlapped connection in progress.
case ERROR_IO_PENDING:
fPendingIO = TRUE;
break;

// Client is already connected, so signal an event.

case ERROR_PIPE_CONNECTED:
if (SetEvent(lpo->hEvent))
break;

// If an error occurs during the connect operation...
default:
{
printf("ConnectNamedPipe failed with %d.n", GetLastError());
return 0;
}
}

return fPendingIO;
}

VOID GetAnswerToRequest(LPPIPEINST pipe)
{
_tprintf( TEXT("[%d] %sn"), pipe->hPipeInst, pipe->chRequest);
StringCchCopy( pipe->chReply, BUFSIZE, TEXT("Default answer from server") );
pipe->cbToWrite = (lstrlen(pipe->chReply)+1)*sizeof(TCHAR);
}[/cpp]

And my compiler options:

[plain]1>Intel C++ Compiler Professional for applications running on IA-32, Version 11.1    Build 20100203 Package ID: w_cproc_p_11.1.060
1>Copyright (C) 1985-2010 Intel Corporation. All rights reserved.
1>icl /c /Od -D WIN32 -D _DEBUG -D _WINDOWS -D _USRDLL -D TEST_EXPORTS -D _WINDLL -D _UNICODE -D UNICODE /EHsc /RTC1 /MDd /GS /fp:fast /FoDebug/ /W3 /ZI /Qstd=c++0x /Qvc9 "/Qlocation,link,C:Program Files (x86)Microsoft Visual Studio 9.0VCbin" .testdll.cpp[/plain]
0 Kudos
vetroxl
Beginner
519 Views
Hi, I'm using the windows 7 sdk... i will try reverting back to the original
0 Kudos
vetroxl
Beginner
519 Views
hi, went to sdk 6 with directx august 2009... it works now. i did not add those DUMMYXX myself... the file had a comment // for compilers that do not support nameless structs/unions
0 Kudos
Reply