|
Axonn Echysttas wrote:
But I don't even know that well the difference between C and C++....
Well, suffice it to say that it looks like you're reading a book on C, which means you're learning a lot of bad habits, if you intend to program C++. malloc and free are C, new and delete are C++. You should never mix them, so the easiest thing is to always use new and delete.
Axonn Echysttas wrote:
a = (LPSTR)cmTime.wHour;
You can't just cast a number to a string. If you must use C functions/want to do things the hard way, then sprintf is what you want, as I said.
Axonn Echysttas wrote:
I'm really a beginner in working with strings but I would really appreciate some help.
The core issues regarding strings:
A string is a pointer, not a single intrinsic type. As such, you need to write memory management code to create and dispose of strings.
You cannot turn something into a string by casting it.
My main beef with the way C++ is taught is that people are taught to do exactly what you're doing, hack around with functions you'll never use again, because you will use CString or ( even better most of the time ) std::string.
Oh, one more - strings are null terminated, so you need a string length of 10 to store a nine character string ( you need room for the null at the end ).
This doesn't even begin to address Unicode strings, etc. I'm just talking about a basic char * here.
Axonn Echysttas wrote:
And even if this is C, I want to know how it's done in C.
Yes, unless what you've written is in a class, everything you're doing is C, as well as being valid C++.
The core answer was in my first post - you need sprintf. I think it's a good thing for you to do it the hard way at some point, just like I think you should write your own list class before using std::list. But it's a tool for learning how stuff works, you won't write production code this way. If I had to do this in the real world, I'd write a SYSTEMTIME inserter for iostreams. But that doesn't mean you're doing anything wrong, you need to walk before you crawl. It's just that I recommend learning C++ first, with the string class, then going back later when you understand the rest to understand what the string class does for you ( because string handling is the major hump in early C++ understanding IMO )
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Axonn Echysttas wrote:
But I don't even know that well the difference between C and C++....
See here.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
First of all, the members of the SYSTEMTIME structure are the actual values, not strings. The following will construct a string according to the format you requested ("HH:MM:SS DD MM"):
char string[32];
SYSTEMTIME cmTime;
GetSystemTime(&cmTime);
sprintf(string,"%02hu:%02hu:%02hu %02hu %02hu",
cmTime.wHour,
cmTime.wMinute,
cmTime.wSecond,
cmTime.wDay,
cmTime.wMonth); You can do the same thing with CString :
CString string;
SYSTEMTIME cmTime;
GetSystemTime(&cmTime);
string.Format("%02hu:%02hu:%02hu %02hu %02hu",
cmTime.wHour,
cmTime.wMinute,
cmTime.wSecond,
cmTime.wDay,
cmTime.wMonth); Note the format: %02hu produces a 2 digit value from a WORD argument (which is unsigned, hence the 'u').
Software Zen: delete this;
|
|
|
|
|
|
Thank you Christian, Gary and Alok. Your advices are exactly what I needed to understand the issue. Gary's code is a perfect example and Alok's links will undoubtely shed light in these matters.
Christian, I knew that strings are pointers. I'm not *THAT* much of a beginner : ). There is a reason why I was doing this the C hard way. I would like to keep my Application as portable as possible. As far as I know CString is a class related to MFC right? Or do Linux versions of C++ also have this class implemented?
Nevertheless, I did do some mistakes and confusions for sure. Thank you for showing me the way.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
I am a relatively competent, mildly skilled programmer, but I have always programmed things in the UNIX/Linux environment, so needless to say I've never used visual C++ or Visual Studio before, and trying to learn it is almost like starting to learn everything all over again. Anyway, I'm writing a program which has to interface with several different devices in order to control them. It just so happens that one of the devices is an intelligent power controller which connects to the computer's COM port and is controlled by simple ASCII commands which are transmitted to the device using the VT100 protocol. The program I'm writing has certain modes which would need different devices to be turned on or off and I would like to be able to send commands to the IPC when needed based upon the status of the program. I have been able to successfully open and close and diagnose the status of all of the COM ports on the computer, I'm just having trouble trying to figure out how to send commands to the IPC and recieve the output using the VT100 protocol. I have some basic communication functions already, but they do not encode the transmission using VT100 or depend on the input/output status of the port. Is there some library in Windows I can use which contains conversion functions or are the only options to either write those myself or buy a software package containing them? If there's anyone out there who has any ideas which could point me in the right direction, I would be very appreciative. Also, if anyone needs more specific information or code, just ask and I'll be happy to post it. Thanks in advance for your help guys!
|
|
|
|
|
As far as I know, there isn't a 'VT100 protocol', per se. The VT100 was a serial computer terminal sold by the late lamented Digital Equipment Corporation many years ago. It was used to communicate with minicomputers over an RS-232 line at a whopping 9600 or 19,200 baud.
That said, you need to find documentation that describes the command set for your IPC. This will tell you what you'll be sending and receiving over the serial port.
As to how, try looking here[^] in the MSDN. While the topic is 'Communication Resources', it includes serial communications.
BTW, since you're coming from the dark side (e.g. *nix), you'll really want to become acquainted with the Microsoft Developer Network[^], or MSDN. It's the second-best resource on the web for Windows programming (first-best, of course, is Code Project[^]!).
Software Zen: delete this;
|
|
|
|
|
I have a service that runs under the system account, not interacting with the desktop. I want it to be able to do something (use the Service Manager to stop a service on another computer) that apparently requires me to be Administrator. I could run my service as Administrator, but I'd rather not do that since it prevents me from changing my mind and making it interact with the desktop, at least for debugging.
Where can I go to learn how my service can impersonate Administrator (the service has access to the login information) while interacting with the Service Manager? I'd appreciate any information. This service is implemented in MFC. Using C# is not an option, nor is learning a lot of COM.
Thanks
|
|
|
|
|
Any information...
Call LogonUser to get token handle.
Call ImpersonateLoggedOnUser using the token handle.
...Do your special stuff...
Call RevertToSelf to go back to your original identity.
|
|
|
|
|
take a look at those pics
before I run the program
http://69.50.187.114/~area51/before.gif
after I run the program = notice I lost my AIM icon.... it overlapped the AIM icon.
http://69.50.187.114/~area51/after.gif
my program is MFC based.
here is code:
void CStart_Button_ChangerDlg::OnClock2()
{
hTaskbar = FindWindowEx(NULL, NULL, _T("Shell_TrayWnd"), NULL);
if (hTaskbar)
{
hTray = FindWindowEx(hTaskbar, NULL, _T("TrayNotifyWnd"), NULL);
hStaticCtrl = ::GetDlgItem(m_hWnd,IDC_CLOCK);
ReplaceClock();
SetTimer(55,1000,0);
}
}
void CStart_Button_ChangerDlg::ReplaceClock()
{
CRect rcTray;
CRect rcStaticCtrl, rcNewStaticCtrl;
::GetWindowRect(hTray, &rcTray);
::GetWindowRect(hClockText, &rcStaticCtrl);
//::SendMessage(hTray, WM_SIZE, 0, 0);
rcNewStaticCtrl.left = rcTray.right - rcTray.left - rcStaticCtrl.Width() - 0;
rcNewStaticCtrl.top = 2;
rcNewStaticCtrl.bottom = rcStaticCtrl.bottom;
rcNewStaticCtrl.right = rcStaticCtrl.right;
CWnd *pStaticCtrlWnd = GetDlgItem(IDC_CLOCK);
pStaticCtrlWnd->MoveWindow(&rcNewStaticCtrl);
pStaticCtrlWnd->ModifyStyle(0, 0, SWP_NOSIZE);
//HWND hWndStatic = GetDlgItem(IDC_CLOCK);
RECT rect;
CBrush hBrush;
CDC* hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(&ps);
hdc->SaveDC();
hBrush.CreateSolidBrush(RGB(0,0,0));
hdc->SelectObject(hBrush);
GetClientRect(&rect);
hdc->Rectangle(&rect);
hdc->SetTextColor(RGB(0,255,255));
hdc->SetBkColor(RGB(0,0,0));
//hdc->SetBkMode(TRANSPARENT);
//hdc->SetBkMode(OPAQUE);
//hdc->RestoreDC(-1);
EndPaint(&ps);
//::InvalidateRect(hTray, NULL, FALSE);
//::InvalidateRect(::GetParent(hTray), NULL, TRUE);
SetWindowLong(hTray, GWL_STYLE, WS_CHILD|WS_VISIBLE|SS_BLACKRECT);
::SetParent(hClockText, hTray);
}
|
|
|
|
|
Hi,
I am doing a project in win32 vc++...dialog based app..
I am getting a string from the user. It is a path. I want to check whether the string is a valid path. Is there any function to check that..
thanks.
|
|
|
|
|
try the function I use
BOOL FileExists(CString strFolderName)
{
return GetFileAttributes(strFolderName) != INVALID_FILE_ATTRIBUTES;
}
My articles
www.stillwaterexpress.com
BlackDice
|
|
|
|
|
Keep in mind that the previous solution will be true for folders AND files.
This one is true only for folders:
BOOL DirectoryExists(CString strFolderName)<br />
{<br />
BOOL bDirectory = FALSE;<br />
<br />
DWORD dwAttributes = GetFileAttributes(strFolderName);<br />
<br />
if( dwAttributes != INVALID_FILE_ATTRIBUTES ){<br />
if( dwAttributes & FILE_ATTRIBUTE_DIRECTORY ){<br />
bDirectory = TRUE;<br />
}<br />
}<br />
<br />
return bDirectory;<br />
}
|
|
|
|
|
Ann66 wrote:
I want to check whether the string is a valid path. Is there any function to check that..
Yes, PathFileExists defined in Shlwapi.h
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi,
Please help me!
My situation is:
I want to write a program which converts Microsoft file (*.doc, *.xls ...) to RepliGo file (*.rgo) for viewing them on Palm.
RepliGo support virtual printer, so I think we can print our documents to that virtual printer, then RepliGo will
convert them to *.rgo.
But my current problem is:
After RepliGo receive your file to print (or convert to .rgo), it shows a Dialog which ask you to name the output file.
I want my program convert a set of files continously, so this Dialog is really a fence to do that purpose.
I don't want to let User see that Dialog.
Please tell me how to prevent that dialog showing and simulate clicking event to OK button on that Dialog to name output file automatically.
Thank a ton.
|
|
|
|
|
You'll need to look into hooking - specifically the hooking of the creation of windows. You should be able to google and fnid plenty on the topic and maybe even some articles on this site.
|
|
|
|
|
Hey,
can anyone help me with this problem:
I've an application with a CTreeCtrl. No I want to chatch the rigth-mousebutton in order to display a contextmenu (depending on selected treeitem).
So I created the follwing eventhandler:
<br />
void CFormTestTree::OnNMRclickTree1(NMHDR *pNMHDR, LRESULT *pResult)<br />
<br />
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);<br />
HTREEITEM hItem = pNMTreeView->itemNew.hItem;<br />
CObject *pObject = (CObject*) m_tcTree.GetItemData(hItem);<br />
The error occours in the last row.
The same code works in the eventhandler-function for the SelChange without any problems.
WHY?
Thanx a lot....
P.S. I forgott to say that the problem is the same when I use the GetMousePos() and HitTest() function to get the Item!
|
|
|
|
|
Hey, I solved the problem with HitTest(). I 've forgotten to convert the point with the ScreenToClient() function. But I have not solved the problem with OnNMRclickTree1 yet...
|
|
|
|
|
Remember, this handler is call whenever the user right-clicks on the tree control. The area clicked on may not be associated with an item, so you need to handle that case.
Software Zen: delete this;
|
|
|
|
|
I am writing a MFC application in VC++. The application needs to be able to open the CD-ROM drive. I am currently using the mciSendString function but i can not get the project configured properly.
i have included windows.h
I played around with linking the winmm.lib library into the project but with no success.
my code:
#include <windows.h>
.
.
.
mciSendString("open cdaudio wait", NULL, 0, NULL);
mciSendString("set cdaudio door open", NULL, 0, NULL);
Can anyone give me an explanation of what i need to include to get mciSendString to work or another way to open the CD-ROM? I've found MSDOS executables that can open the CD_ROM drive but i can not find code on how to do this.
|
|
|
|
|
jet915 wrote:
The application needs to be able to open the CD-ROM drive.
Try:
MCI_OPEN_PARMS mciOpenParms;
MCI_SET_PARMS mciSetParms;
DWORD dwResult;
mciOpenParms.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
dwResult = mciSendCommand(NULL, MCI_OPEN, MCI_WAIT | MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID, (DWORD) ((LPMCI_OPEN_PARMS) &mciOpenParms));
if (0 == dwResult)
{
dwResult = mciSendCommand(mciOpenParms.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, (DWORD) ((LPMCI_SET_PARMS) &mciSetParms));
...
}
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Hello can anyone tell me how to access shared folders on Lan for copying something in them.
Be FaithFull To Your Work.
|
|
|
|
|
Use the \\server\share convention, also known as UNC.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Which class of VC6 is used for the above job , i mean getting the list of shared folders on LAN and then copying something(Daily schedule for every employee) in it.I want to do this job programmatically.
Please Help.
Be FaithFull To Your Work.
|
|
|
|
|
Usman Tasleem Akshaf wrote:
Which class of VC6 is used for the above job
Any function or method that can handle UNC will work.
Usman Tasleem Akshaf wrote:
i mean getting the list of shared folders on LAN...
Use the WNetOpenEnum() and WNetEnumResource() pair.
Usman Tasleem Akshaf wrote:
...and then copying something(Daily schedule for every employee) in it.
Use CopyFile() or SHFileOperation() .
Usman Tasleem Akshaf wrote:
Be FaithFull To Your Work.
BTW, there's only one 'l' in faithful, assuming it's not meant to be misspelled.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|