|
Hi, I had the same problem, but I also needed the delimiters to be strings. Here's a function I wrote to do this:
std::string FindEnclosedString( const std::string& src, const std::string& left_guard, const std::string& right_guard )
{
int start, stop;
std::string dest;
start = src.find(left_guard);
if (start != std::string::npos)
{
start += left_guard.size();
stop = src.find(right_guard, start);
if (stop != std::string::npos)
stop -= start;
dest = src.substr(start, stop);
}
return dest;
}
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
I want to send a Command string to the modem port (COM3).
I have created a file with CreateFile()
then what should be the parameters to the WriteFile() function to send a string to the COM3 port ... ?
Apurv
|
|
|
|
|
Have you tried passing the file name as "COM3" (no extension, no root directory or absolute path required). That must give you direct access to the port.
Besides that, what is attached at COM3 port? Are you aware that some devices might require you to follow a specific text pattern (usually a series of ASCII keywords) if you want to send it a command? For instance, a printer usually follows the EPSON escape code sequences. I think you need to try specifying the file name as "COM3", as I already said and if it doesn't give you the desired results, you will need to provide more details.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
i m configuring the COM3 port as follows ...
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM3");
DWORD dwBytesWritten = 0;
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hCom == INVALID_HANDLE_VALUE)
{
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
dcb.BaudRate = CBR_57600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
_tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
Apurv
|
|
|
|
|
i m configuring the COM3 port as follows ...
and it is getting configured correctly ....
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM3");
DWORD dwBytesWritten = 0;
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hCom == INVALID_HANDLE_VALUE)
{
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
dcb.BaudRate = CBR_57600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
_tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
Apurv
|
|
|
|
|
Good. Now, you can treat it as a file and write your commands as if you were writing it into a file. You may use WriteFile[^] for instance. Or you have a trouble somewhere else?
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
Hi all,
I have this project which is downloading product updates over the internet. What I do first is that before I can go to the second download the first download must be finished. Now I have decided to create a thread so I can download updates simultaneously.
Let me show a bit of the code:
while (m_bIsRunning)
{
Sleep(5000);
if (isUpdateTime())
{
CSiteCode m_CSiteCode;
RMproduct* MPoint = new RMproduct(L"ManagePoint",ManagepointService, ManagepointVersion);
if (isPrdInstalled(MPoint))
{
GetVersion(MPoint);
MPoint->m_SiteCode = m_CSiteCode.GetMPSiteCode(MPoint->m_SerialNo.c_str());
dloadAvailable = DoCheckForProductUpdates(MPoint);
if(dloadAvailable)
{
GetUpdate(MPoint);
}
}
delete MPoint;
RMproduct* ReRite = new RMproduct(L"ReRite",ReriteService, ReriteVersion);
if (isPrdInstalled(ReRite))
{
GetVersion(ReRite);
ReRite->m_SiteCode = m_CSiteCode.GetReRiteSiteCode(ReRite->m_SerialNo.c_str());
dloadAvailable = DoCheckForProductUpdates(ReRite);
if(dloadAvailable)
{
GetUpdate(ReRite);
}
}
delete ReRite;
RMproduct* ReRiteAsian = new RMproduct(L"ReRite",ReriteAsianService, ReriteAsianVersion);
if (isPrdInstalled(ReRiteAsian))
{
GetVersion(ReRiteAsian);
ReRiteAsian->m_SiteCode = m_CSiteCode.GetReRiteAsianSiteCode(ReRiteAsian->m_SerialNo.c_str());
dloadAvailable= DoCheckForProductUpdates(ReRiteAsian);
if(dloadAvailable)
{
GetUpdate(MPoint);
}
}
delete ReRiteAsian;
}
}
This is service btw. Now the function GetUpdate call a function called downloadfile:
bool CUpdateServiceModule::DownloadFile(RMUpdates* prd)
{
bool result = false;
int Data_Of_Thread_1 = 1;
HANDLE Handle_Of_Thread_1 = 0;
HANDLE Array_Of_Thread_Handles[3];
Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, ((void*)prd), 0, NULL);
if ( Handle_Of_Thread_1 == NULL) ExitProcess(Data_Of_Thread_1);
Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
WaitForMultipleObjects( 3, Array_Of_Thread_Handles, TRUE, INFINITE);
CloseHandle(Handle_Of_Thread_1);
Sleep( 100 );
return result;
}
This download file is creating a thread that calls the downloadthread global function which downloads the update. The behavior of this downloadthread function is that even the service stops once it is restored it will start the download where it ended:
DWORD WINAPI Thread_no_1( PVOID PrdPtr )
{
DownloadThread((void*)PrdPtr);
return 0;
}
void DownloadThread(PVOID PrdPtr)
{
RMUpdates *ParamPtr;
ParamPtr = (RMUpdates *) PrdPtr;
FILE * pFile = NULL;
DWORD dwPacketSize =5;
PBYTE pBuffer = new BYTE[dwPacketSize * 1024];
double dOffsetToSeek =0;
std::wstring strTmpFileName = ParamPtr->savePath.c_str();
strTmpFileName.append(L".tmp");
std::wstring strFileNameAtLocalMachine= ParamPtr->savePath.c_str();
int res = DeleteFile(strFileNameAtLocalMachine.c_str());
double dCurrentFileSize;
if(IfLocalFileExist(strTmpFileName.c_str(), &dCurrentFileSize))
{
dOffsetToSeek = dCurrentFileSize;
}
CString strRangeQuest;
if(dCurrentFileSize>0){
strRangeQuest.Format( _T("%sRange: bytes=%d-\r\n"), szHeaders,static_cast<ULONGLONG>(dCurrentFileSize));
}
else
strRangeQuest = szHeaders;
if (!(pFile = _wfopen (strTmpFileName.c_str(), L"a+b" ) ) )
{
return;
}
try
{
CInternetSession Session(_T("UpdateService"));
CHttpConnection Connection1(Session,ParamPtr->link.c_str());
BYTE Buffer[4096];
CHttpFile dFile(Session,ParamPtr->link.c_str());
CInternetFile::CInfo Info(dFile);
dFile.SendRequest(strRangeQuest);
DWORD dwRead;
for (dwRead = 1 ; dwRead; )
{
dFile.Read(Buffer,sizeof(Buffer),dwRead);
if (dwRead>0)
fwrite(Buffer, sizeof (char), dwRead , pFile);
}
fclose (pFile);
BOOL moved = MoveFile(strTmpFileName.c_str(), strFileNameAtLocalMachine.c_str());
if(moved)
{
EmailUser(strFileNameAtLocalMachine.c_str());
}
DeleteFile(strTmpFileName.c_str());
HKEY hkey;
TCHAR szKey[256];
int nValue = 0;
szKey[0] = _T('\0');
_tcscpy(szKey, ServiceRegEntry);
_tcscat(szKey, UPDATEMGR);
_tcscat(szKey, ServiceParam);
_tcscat(szKey, _T("\\"));
_tcscat(szKey,ParamPtr->prdName.c_str());
_tcscat(szKey, _T("\\"));
_tcscat(szKey,ParamPtr->nameVer.c_str());
if (RMRegOpenKeyEx(HKEY_LOCAL_MACHINE,szKey,0,KEY_QUERY_VALUE | KEY_SET_VALUE,&hkey) == ERROR_SUCCESS)
{
nValue = 1;
RMRegSetValueEx(hkey,UPDATESTAT,0,REG_DWORD,(BYTE*)&nValue,sizeof(nValue));
RMRegCloseKey(hkey);
}
}
catch(CInternetException& err)
{
err.GetErrorMessage();
}
}
Now what I would like to know is that while the first thread is downloading the first update will it go to the next update and download it? Or still my code will just wait for the first thread to download the first update? I am having a hard time debugging this as I dont know how the process splits.
Thanks,
Jayjay
|
|
|
|
|
monsieur_jj wrote: HANDLE Array_Of_Thread_Handles[3]; // Aray to store thread handles
Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, ((void*)prd), 0, NULL);
if ( Handle_Of_Thread_1 == NULL) ExitProcess(Data_Of_Thread_1);
// Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects()
Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
Here you're creating a uninitialized handle array, and then setting only the first member. The rest handles will contain junk values.
monsieur_jj wrote: // Wait until all threads have terminated.
WaitForMultipleObjects( 3, Array_Of_Thread_Handles, TRUE, INFINITE);
And here you are waiting for all handles include junk handle values which will finally result in error.
monsieur_jj wrote: Now what I would like to know is that while the first thread is downloading the first update will it go to the next update and download it?
You've just created a single thread.
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
Hi Jijo,
Thanks for the reply
so I have changed it to:
HANDLE Array_Of_Thread_Handles[3];
Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, ((void*)prd), 0, NULL);
if ( Handle_Of_Thread_1 == NULL) ExitProcess(Data_Of_Thread_1);
Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
WaitForMultipleObjects( 1, Array_Of_Thread_Handles, TRUE, INFINITE);
Are you saying it will just finish downloading of that update even if its on a thread and will not go downloading the other updates?
thanks,
Jayjay
|
|
|
|
|
What other updates? You've only called CreateThread() once.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark,
Here is the deal
if (isUpdateTime())
{
RMproduct* MPoint = new RMproduct(L"ManagePoint",ManagepointService, ManagepointVersion);
if (isPrdInstalled(MPoint))
{
GetVersion(MPoint);
MPoint->m_SiteCode = m_CSiteCode.GetMPSiteCode(MPoint->m_SerialNo.c_str());
dloadAvailable = DoCheckForProductUpdates(MPoint);
if(dloadAvailable)
{
GetUpdate(MPoint);
}
}
delete MPoint;
RMproduct* MPoint2 = new RMproduct(L"ManagePoint",ManagepointService, ManagepointVersion);
if (isPrdInstalled(MPoint2))
{
GetVersion(MPoint2);
MPoint2->m_SiteCode = m_CSiteCode.GetMPSiteCode(MPoint2->m_SerialNo.c_str());
dloadAvailable = DoCheckForProductUpdates(MPoint2);
if(dloadAvailable)
{
GetUpdate(MPoint)2;
}
}
delete MPoint;
RMproduct* MPoint3 = new RMproduct(L"ManagePoint",ManagepointService, ManagepointVersion);
if (isPrdInstalled(MPoint))
{
GetVersion(MPoint3);
MPoint3->m_SiteCode = m_CSiteCode.GetMPSiteCode(MPoint3->m_SerialNo.c_str());
dloadAvailable = DoCheckForProductUpdates(MPoint3);
if(dloadAvailable)
{
GetUpdate(MPoint3);
}
}
delete MPoint3;
}
<pre>
The GetUpdate function calls the function DownloadFile():
<pre>
bool CUpdateServiceModule::DownloadFile(RMUpdates* prd)
{
bool result = false;
int Data_Of_Thread_1 = 1;
HANDLE Handle_Of_Thread_1 = 0;
HANDLE Array_Of_Thread_Handles[1];
Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, ((void*)prd), 0, NULL);
if ( Handle_Of_Thread_1 == NULL) ExitProcess(Data_Of_Thread_1);
Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
WaitForMultipleObjects( 1, Array_Of_Thread_Handles, TRUE, INFINITE);
CloseHandle(Handle_Of_Thread_1);
Sleep( 100 );
return result;
}
Thing is MPoint2 and MPoint3 are not sure to exist if they dont the code will only download one update if they exist it may download 2 or 3 updates. But for sure one exists. How must I create a thread in this type of event?
Thanks,
Jayjay
|
|
|
|
|
Your code as show does nothing multi threaded.
The DownloadFile() method creates a thread, but since it
waits for the thread to complete, it is effectively a complicated
way to do work on ONE thread.
I'm not entirely sure what you are trying to do.
Do you want a separate thread for each download and then
a way to wait for all the threads to complete before the
app proceeds?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark,
I want a separate thread for each download and maybe wait for all of it to finish or let the app proceed while downloading whichever is ok with me.
Thanks,
Jayjay
|
|
|
|
|
is there an x64 version of HTMLHELP.LIB ?
i'm trying to migrate a project to x64 and i'm getting an unresolved external on HtemlHelpA.
::HtmlHelpA(NULL, path, HH_DISPLAY_TOPIC, 0);
the function is defined in htmlhelp.h, but it doesn't seem to be in the .LIB .
i've done all the googlin i can take.
|
|
|
|
|
Hi all,
in my application i want to read all the key and data values of some registry folder so i am using this RegEnumValue function but when i kept this function in for loop it is reading few values and failing in the middle..
can anybody suggest me the reason for this i attach the code i have used for this...
<br />
CString names[100],values[100];<br />
CString Accesspath;<br />
char szBufferReturn[MAX_VALUE_NAME];<br />
char szData[MAX_VALUE_NAME];
DWORD nDataSize = MAX_PATH;
DWORD dwType = 0;<br />
DWORD nBufferReturnSize = sizeof(szBufferReturn);<br />
long nReturnCode = 0;<br />
int i=0;<br />
Accesspath = "SOFTWARE\\XYZ\\ImageScanner\\checkfinal";<br />
<br />
HKEY hKey;<br />
for( i =0 ;i< 10; i++)<br />
{<br />
<br />
if( ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,Accesspath,0, KEY_ALL_ACCESS,&hKey))<br />
{<br />
if ((nReturnCode = RegEnumValue(hKey, i,<br />
szBufferReturn,<br />
&nBufferReturnSize,<br />
NULL,<br />
&dwType,<br />
(LPBYTE)szData, <br />
&nDataSize)) == ERROR_SUCCESS)<br />
{<br />
<br />
<br />
names[i]=szBufferReturn;<br />
MessageBox(names[i]);<br />
nBufferReturnSize = sizeof(szBufferReturn);<br />
<br />
if (dwType == REG_SZ)<br />
{<br />
values[i]=szData;<br />
<br />
}<br />
else<br />
{<br />
printf("Identifier contains a non-string'\n\n");<br />
MessageBox("failed");<br />
}<br />
}<br />
else<br />
{<br />
MessageBox("failed");<br />
}<br />
RegCloseKey(hKey);<br />
}<br />
else<br />
{<br />
MessageBox("not openeded");<br />
}<br />
<br />
<br />
}
<br />
<br />
|
|
|
|
|
hariakuthota wrote: ...and failing in the middle...
Are we supposed to guess where?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Check the error code returned by the failing function. It can shed more light into the problem.
You can also check the last error code. Call GetLastError() or add @err,hr to watch window to get the last error.
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
Most likely the problem is that the call to RegEnumValue will change the value of nDataSize (that's an IN/OUT parameter).
You need to reset it before each call.
Hope that helps.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
I want to have 2 different configurations that use two different versions. For example, Release configuration I want to be v6.0 and ReleaseOlder to be v4.6 Anyway to do this? I"m using VS2008
Thanks
-C
|
|
|
|
|
Just put an #ifdef #endif block in your resource file.
e.g
----- Resource.h ---------
#ifndef resource_h
#define resource_h
#define use_manifest
#endif
----- Resource.rc --------
#include <windows.h>
#include "resource.h"
#ifdef use_manifest
LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT
1 RT_MANIFEST ".\\manifest.xml"
#endif
This code would require that InitCommonControls was called somewhere and that the appropriate library was linked. You could use the use_manifest #define to conditionally include <commctrl.h> & call InitCommonControls.
modified on Thursday, August 28, 2008 1:38 PM
|
|
|
|
|
You'll need to have a preprocessor constant that's unique to each config. For this example, imagine it's debug vs. release. Debug builds have _DEBUG defined, while release builds have NDEBUG .
Right-click on the VS_VERSION_INFO resource and pick Properties. On the Condition line, enter _DEBUG .
Then make a copy of that resource, and in its properties, enter NDEBUG on the Condition line. After you set that condition, you can change the ID back to VS_VERSION_INFO , and you're done.
(This is in 2005 but it should be similar if not the same in 2008)
|
|
|
|
|
I am trying to read the contents of a txt file
using the internet session in an activex session
with no reading results, I have tried the same
method in an application and it works fine.
Does anybody know whats going on.
thanks v much simon
|
|
|
|
|
Hi all,
I am writing an application that checks the correct functionality of a keyboard.
I am doing so by overriding the PreTranslateMessage(MSG *pMsg).
In my PreTranslateMessage i am catching all the WM_KEYDOWN and WM_SYSKEYDOWN messages and read from the wparam which key was pressed .
I have 2 problems:
1.
When pressing the start button (VK_LWIN), the start button is actually pressed and the start menu is opened. The menu is being opened even if i put a break point in the beginning of my PreTranslateMessage. I just want to know when the start button was pressed and i dont want it to open the start menu.
2.
It seems that the print screen button doesn't generate any of the 2 messages that i process in the PreTranslateMessage. what is the window message that is generated when i press the print screen key?
Thanks
Simon
|
|
|
|
|
Have you tried using SetWindowsHookEx() with the WH_KEYBOARD or WH_KEYBOARD_LL hook procedure? Look at the docs here.
Regards,
--Perspx
"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia
Introduction to Object-Oriented JavaScript
|
|
|
|
|
Those two buttons, and a couple of others are special. They're not forwarded to your application at all, instead Windows processes these key combinations internally. It's like Ctrl+Alt+Del
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|