Hi Everyone.
Apologies for the length of my first post but this one has perplexed me an bit in my project.
Im creating an Windows Media Player Visualization in VS2008 and am drawing an blank on the below.

What i need to do with the below code is to create an array of the CreateFile handles which i can then use in the WriteFile function lower down. The problem im having is that i cant seem to get it to follow the usual syntax.
e.g.

HANDLE comports[10];
comports[0] = CreateFile("\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

(this results in VS2008 trying to default comports[0] to an int)

This is the code i have so far (which does work for an single com port, but im working on having it do the same on multiple)

#include "StdAfx.h"
#include "stdio.h"
#include "stdlib.h"
#include "USBGlowLight.h"
#include "CPropertyDialog.h"
#include "math.h"
#include "string.h"

INT iDelayVal = 150;
DWORD	bytes_read	= 0;    // Number of bytes read from port
DWORD	bytes_written = 0;    // Number of bytes written to the port

// Open COM port(s)
HANDLE comport=CreateFile("\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DCB dcb = {0};

COMMTIMEOUTS CommTimeouts;
STDMETHODIMP CUSBGlowLight::Render(TimedLevel *pLevels, HDC hdc, RECT *prc)
{
	    COLORREF mycolor;
	COLORREF oldmycolor;
	//paint old Color on screen
	HBRUSH hOldBrush = ::CreateSolidBrush( oldmycolor );
	::FillRect( hdc, prc, hOldBrush );
			int y = 0;
			//Red colour
			for (int x = 1; x < ((SA_BUFFER_SIZE)/3); ++x)
            {
                y = y + static_cast<int>(256.0f * pLevels->frequency[0][x - (prc->left - 1)]); 
            }
			int myRlevel = y / ((SA_BUFFER_SIZE)/3);
			y = 0;
			
			//Green colour
			for (int x = ((SA_BUFFER_SIZE)/3); x < (((SA_BUFFER_SIZE)/3)*2); ++x)
            {
                y = y + static_cast<int>(256.0f * pLevels->frequency[0][x - (prc->left - 1)]);
            }
			int myGlevel = y / ((((SA_BUFFER_SIZE)/3)*2) - ((SA_BUFFER_SIZE)/3));
			y = 0;
			
			//Blue colour
			for (int x = (((SA_BUFFER_SIZE)/3)*2); x < (SA_BUFFER_SIZE-1); ++x)
            {
               y = y + static_cast<int>(256.0f * pLevels->frequency[0][x - (prc->left - 1)]);
            }
			int myBlevel = y / ((SA_BUFFER_SIZE-1) - (((SA_BUFFER_SIZE)/3)*2));
			y = 0;

	//convert to hex code
	BYTE RED = myRlevel;// - (256.0f/2);
	BYTE GREEN = myGlevel;// - (256.0f/2);
	BYTE BLUE = myBlevel;// - (256.0f/2);
	COLORREF c = RGB(RED, GREEN, BLUE);

	char str[8];
	sprintf(str, "#%06X", SwapBytes(c));
	
	//Choose whether to show the Visual or display Black
    switch (m_nPreset)
    {
    case PRESET_BLANK:
        {
			mycolor = m_clrForeground;
        }
		break;
	case PRESET_SHOW:
		{
			mycolor = RGB( myRlevel, myGlevel, myBlevel);;
		}
		break;
    }
	
	//Copy data to the outputbuffer ( for the comm port)
    char OUTBUFFER[400];
    strcpy(&OUTBUFFER[0], str);

	//write data to comm port
	WriteFile(comport, &OUTBUFFER, 7, &bytes_written, NULL);
	
	//paint Color on screen
	HBRUSH hNewBrush = ::CreateSolidBrush( mycolor );
	::FillRect( hdc, prc, hNewBrush );
	
	oldmycolor = mycolor;
	//Sleep in between visuals
	Sleep(iDelayVal);

    if (hNewBrush)
    {
	    ::DeleteObject( hNewBrush );
    }

    return S_OK;
}

Apologies again if the code above looks quite messy, It's an work in progress to clean it up, And again, thanks for any help that anyone can give me :)

Steve

That first code snippet compiled ok for me after adding another \ escape character at the start comports[0] = CreateFile("\\\\.\\COM6", /* blabla */

That first code snippet compiled ok for me after adding another \ escape character at the start comports[0] = CreateFile("\\\\.\\COM6", /* blabla */

Thanks for the above.
Unfortunately when i try to compile it i get the following error
"error C2466: cannot allocate an array of constant size 0"

This was when i entered the below as the code.

HANDLE comports[10];
comports[0]=CreateFile("\\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

Here is the complete program

#include <windows.h>



int main() {
HANDLE comports[10];
comports[0] = CreateFile("\\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);	
return 0;
}

Thanks for the above.
Unfortunately when i try to compile it i get the following error
"error C2466: cannot allocate an array of constant size 0"

This was when i entered the below as the code.

HANDLE comports[10];
comports[0]=CreateFile("\\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

Ignore that, I've found the compiler wont allow me to assign the first createfile into comports[0]. It will let me use comports[1] though.

Now though its givin me error C4430 on line 14. Its trying to change comports[1] into an default-int.

did you include windows.h as I posted it???

did you include windows.h as I posted it???

Yep, included the windows.h header exactly as you've written it.

Well, I am using VC++ 2008 Express and the program I posted compiles without any errors or warnings. I can only guess that you didn't set up your project correctly. Try creating a new console project, then change the project properties so that it doesn't use UNICODE.

Well, I am using VC++ 2008 Express and the program I posted compiles without any errors or warnings. I can only guess that you didn't set up your project correctly. Try creating a new console project, then change the project properties so that it doesn't use UNICODE.

created an default visualization using the Wizard (as MSDN states)
Changed Project to not use Unicode.

Still doesnt Work. Gets below errors

.\USBGlowLight.cpp(24) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
.\USBGlowLight.cpp(24) : error C2369: 'comports' : redefinition; different subscripts
.\USBGlowLight.cpp(23) : see declaration of 'comports'
.\USBGlowLight.cpp(24) : error C2440: 'initializing' : cannot convert from 'HANDLE' to 'int [1]' There are no conversions to array types, although there are conversions to references or pointers to arrays

If You're willing, i've archived the full solution and made it downloadable so you can see the full thing. You can download the full solution from the below link so you can see exactly what i've done (and hopefully where its breaking)
http://www.4shared.com/file/193041373/740b0d64/USBGlowLight.html

Ok, Managed to figure one thing out but its not an ideal solution. Below is how im declaring the array with the File(Com port) Handles.

CONST INT iCommPortCount = 5;
HANDLE comports[iCommPortCount] = {CreateFile("\\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL)};

The above works and the handle is opened and stored in array[0]. However it still seems to have an issue if i want to do the below.

CONST INT iCommPortCount = 5;
HANDLE comports[iCommPortCount];
comports[0] = CreateFile("\\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
comports[1] = CreateFile("\\\\.\\COM7", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
comports[2] = CreateFile("\\\\.\\COM8", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
comports[3] = CreateFile("\\\\.\\COM9", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
comports[4] = CreateFile("\\\\.\\COM10", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

This gives me the below on the first value of 'comports'

"error C2466: cannot allocate an array of constant size 0"

zip up the project and attach it to your post. But first delete all compiler-generated files, especially those huge precompiled header files.

However it still seems to have an issue if i want to do the below.

CONST INT iCommPortCount = 5;
HANDLE comports[iCommPortCount];
comports[0] = CreateFile("\\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
comports[1] = CreateFile("\\\\.\\COM7", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
comports[2] = CreateFile("\\\\.\\COM8", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
comports[3] = CreateFile("\\\\.\\COM9", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
comports[4] = CreateFile("\\\\.\\COM10", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

This gives me the below on the first value of 'comports'

"error C2466: cannot allocate an array of constant size 0"

Are you doing that in the global scope, i.e. outside of any function? If you are, then move (at least) those CreateFile() calls inside a function.

Your problem is that the line

comports[0]=CreateFile("\\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

is outside any function. Thus is is treated by compiler as a definition of a global array of size 0 with an initializer.
Move it into main, or some other appropriate function.

PS: Oops. Missed the second page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.