From: Jessica C. <jes...@gm...> - 2007-12-20 15:02:39
|
yep.. i will change 4096*256=1M part. that is too big for saving serial port info. the function of strdup is equal to strlen() + malloc() + memcpy()? so, i don't need to do memalloc for strSerialList as what i did, since strdup() will do? is this a correct understanding? On Dec 20, 2007 9:23 AM, Per Westermark <pw...@ia...> wrote: > Now, you are preallocating 4096 bytes for 256 serial ports. That is 1MB of > memory. That is not something you want in production code. > > My change with strdup() would have performed a strlen() + an optimum-sized > malloc() + memcpy() for any detected serial ports, i.e. it will end up > needing at the most a couple of hundred bytes of memory for the names. > > /pwm > > On Thu, 20 Dec 2007, Jessica Chen wrote: > > > Hi, Per, > > > > Thanks. I asked my friend to see. He gave me the suggestion. The > > RegEnumValue can recognize the com and USB->COM. The problem solved > now, I > > got the correct result: COM1(com), COM7(usb->com). In front of > > RegEnumValue, add: > > dwName = sizeof(Name); > > dwSizeofPortName = sizeof(szPortName); > > So the whole code is as follows (a bug in it: didn't release memalloc, > but > > doesn't influence the result here). > > > > #include <windows.h> > > #include <stdio.h> > > #include <tchar.h> > > > > int main(void) > > { > > int z; > > char *strSerialList[256]; > > for (z=0; z<=256; z++) > > { > > strSerialList[z] = (char*)malloc(4096*sizeof(char)); > > } > > HKEY hKey; > > > > if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, > > TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"), > > 0, > > KEY_READ, > > &hKey) != ERROR_SUCCESS) > > { > > return -1; > > } > > > > int i=0; > > CHAR Name[4096]; > > UCHAR szPortName[4096]; > > LONG Status; > > DWORD dwIndex = 0; > > DWORD dwName; > > DWORD dwSizeofPortName; > > DWORD Type; > > dwName = sizeof(Name); > > dwSizeofPortName = sizeof(szPortName); > > > > do > > { > > dwName = sizeof(Name); > > dwSizeofPortName = sizeof(szPortName); > > Status = RegEnumValue(hKey, dwIndex++, Name, &dwName, NULL, > &Type, > > szPortName, &dwSizeofPortName); > > printf("Name=%s,szPortName=%s \n", Name, szPortName); > > if((Status == ERROR_SUCCESS) ) > > { > > strcpy(strSerialList[i],(char*)szPortName); > > //strSerialList[i] = (char*) szPortName; > > printf("%s ",strSerialList[i]); > > printf("i=%d,dwIndex=%d\n",i,dwIndex); > > i++; > > } > > } while((Status == ERROR_SUCCESS) ); > > RegCloseKey(hKey); > > > > printf("\n\n"); > > for(int j=0;j<i;j++) > > { > > printf("%s\n",strSerialList[j]); > > } > > > > return 0; > > } > > > > > > > > On Dec 19, 2007 6:02 PM, Per Westermark <pw...@ia...> wrote: > > > > > On an old test machine I have, the original code produces: > > > COM1 > > > COM1 > > > COM1 > > > COM1 > > > COM1 > > > COM1 > > > > > > After adding strdup(), I get: > > > COM5 > > > COM6 > > > COM7 > > > COM7 > > > COM4 > > > COM1 > > > > > > Are you sure that your change was correct, and that you remembered to > > > recompile? > > > > > > One interesting thing here is that COM7 is printed twice, while my > much > > > used COM3 is not enumerated. > > > > > > The two COM7 seems to be a stupid M$ bug with an USB serial adapter :( > > > > > > /pwm > > > > > > On Wed, 19 Dec 2007, Jessica Chen wrote: > > > > > > > Hi, Per, > > > > > > > > for your second method, i modified like this. i doesn't solve the > > > > problem. the result is the same as before. > > > > for the first method, i tried to change strSerialList[i] = > > > > (char*)(szPortName); to strSerialList[i] = > strdup((char*)(szPortName));, > > > but > > > > it seems doesn't work too. :-( > > > > > > > > Regards, > > > > Jessica > > > > > > > > do > > > > { > > > > Status = RegEnumValue(hKey, dwIndex++, Name, &dwName, NULL, > > > &Type, > > > > szPortName, &dwSizeofPortName); > > > > if((Status == ERROR_SUCCESS) || (Status == ERROR_MORE_DATA)) > > > > { > > > > strSerialList[i] = (char*)(szPortName); > > > > printf("%s\n",strSerialList[i]); > > > > i++; > > > > } > > > > } while((Status == ERROR_SUCCESS) || (Status == > ERROR_MORE_DATA)); > > > > RegCloseKey(hKey); > > > > > > > > On Dec 19, 2007 2:44 PM, Per Westermark <pw...@ia...> > wrote: > > > > > > > > > The test code is buggy. It uses the same buffer to retrieve the > device > > > > > names, which means that the name will be overwritten. > > > > > > > > > > A quicky (with a memory leak that doesn't matter here) is to do > > > > > strSerialList[i] = strdup((char*)szPortName); > > > > > > > > > > Another alternative is to print the devices one-by-one directly > when > > > they > > > > > are found. > > > > > > > > > > /pwm > > > > > > > > > > On Wed, 19 Dec 2007, Jessica Chen wrote: > > > > > > > > > > > Name Type Data > > > > > > (Default) REG_SZ (value not set) > > > > > > \Device\Serial0 REG_SZ COM1 > > > > > > \Device\U2SPORT1 REG_SZ COM7 > > > > > > > > > > > > if i used on my computer, i can see: > > > > > > COM1 > > > > > > COM1 > > > > > > > > > i> > > when i used my .exe on my colleagues computer. > > > > > > he has 2 serial port and 1 usb->serial. > > > > > > but we can only see "COM4" for 3 times. > > > > > > > > > > > > so at least, the code can recognize how many COM on computer, > > > whatever > > > > > real > > > > > > com port or usb-com. > > > > > > but cannot read right com port number. > > > > > > > > > > > > > > > > > > On Dec 19, 2007 1:57 PM, Rafael Menezes <so...@gm...> > wrote: > > > > > > > > > > > > > Hi Jessica. > > > > > > > > > > > > > > Can you, for start, list what is on > > > "HARDWARE\\DEVICEMAP\\SERIALCOMM" > > > > > in > > > > > > > your computer? > > > > > > > > > > > > > > Regards > > > > > > > Rafael > > > > > > > > > > > > > > > > > > > > > On 19/12/2007, Jessica Chen <jes...@gm...> wrote: > > > > > > > > > > > > > > > > Hi, thanks for ur email, Rafael. > > > > > > > > > > > > > > > > I researched myself. > > > > > > > > I wrote a code. I will list it as follows. > > > > > > > > The problem seems it can recognize the real serial com port > (Com > > > 1 > > > > > in my > > > > > > > > computer, registry). > > > > > > > > But on my computer, I have a USB->serial com port (Com 7 in > my > > > > > > > > computer, registry). > > > > > > > > The code cannot recognize the USB->serial com port. > > > > > > > > Is there any experienced persons can give any idea about > that? > > > > > > > > > > > > > > > > #include <windows.h> > > > > > > > > #include <stdio.h> > > > > > > > > #include <tchar.h> > > > > > > > > > > > > > > > > int main(void) > > > > > > > > { > > > > > > > > char *strSerialList[256]; > > > > > > > > HKEY hKey; > > > > > > > > > > > > > > > > if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, > > > > > > > > > TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"), > > > > > > > > 0, > > > > > > > > KEY_READ, > > > > > > > > &hKey) != ERROR_SUCCESS) > > > > > > > > { > > > > > > > > return -1; > > > > > > > > } > > > > > > > > > > > > > > > > int i=0; > > > > > > > > CHAR Name[25]; > > > > > > > > UCHAR szPortName[25]; > > > > > > > > LONG Status; > > > > > > > > DWORD dwIndex = 0; > > > > > > > > DWORD dwName; > > > > > > > > DWORD dwSizeofPortName; > > > > > > > > DWORD Type; > > > > > > > > dwName = sizeof(Name); > > > > > > > > dwSizeofPortName = sizeof(szPortName); > > > > > > > > > > > > > > > > do > > > > > > > > { > > > > > > > > Status = RegEnumValue(hKey, dwIndex++, Name, &dwName, > > > NULL, > > > > > > > > &Type, > > > > > > > > szPortName, &dwSizeofPortName); > > > > > > > > if((Status == ERROR_SUCCESS) || (Status == > > > ERROR_MORE_DATA)) > > > > > > > > { > > > > > > > > strSerialList[i] = (char*)(szPortName); > > > > > > > > i++; > > > > > > > > } > > > > > > > > } while((Status == ERROR_SUCCESS) || (Status == > > > > > ERROR_MORE_DATA)); > > > > > > > > RegCloseKey(hKey); > > > > > > > > > > > > > > > > for(int j=0;j<i;j++) > > > > > > > > { > > > > > > > > printf("%s\n",strSerialList[j]); > > > > > > > > } > > > > > > > > > > > > > > > > return 0; > > > > > > > > } > > > > > > > > > > > > > > > > > > > > > > > > On Dec 19, 2007 12:34 PM, Rafael Menezes <so...@gm...> > > > wrote: > > > > > > > > > > > > > > > > > You can send mails. Can you search on google? > > > > > > > > > > > > > > > > > > > > > http://www.google.com.br/search?hl=pt-BR&q=c%2B%2B+com+port&meta = > > > > > > > > > > > > > > > > > > > > > > > > > > > http://www.google.com.br/search?num=30&hl=pt-BR&q=c%2B%2B+serial+port&meta > > > > > > > > > = > > > > > > > > > > > > > > > > > > > > > > > > > > > On 19/12/2007, Jessica Chen <jes...@gm... > > wrote: > > > > > > > > > > > > > > > > > > > is there anyone have the experience for writing code of > > > getting > > > > > the > > > > > > > > > > serial port info from windows? > > > > > > > > > > can u give me an sample code? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ------------------------------------------------------------------------- > > > > > > > > > > > > > > > > > > > > SF.Net email is sponsored by: > > > > > > > > > > Check out the new SourceForge.net Marketplace. > > > > > > > > > > It's the best place to buy or sell services > > > > > > > > > > for just about anything Open Source. > > > > > > > > > > > > > > > > > > > http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > > > > > > > Dev-cpp-users mailing list > > > > > > > > > > Dev...@li... > > > > > > > > > > TO UNSUBSCRIBE: > > > http://www23.brinkster.com/noicys/devcpp/ub.htm > > > > > > > > > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > > > > Regards > > > > > > > > > Rafael Menezes > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > > Regards > > > > > > > Rafael Menezes > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > |