|
This is just too funny. sigh, in a sad sort of funny....
So, I have managed to muddle along as there simply is no time to re-write this app. Yet. I can get it to run in the debugger under Windows 7, so that's what I've been doing.
Fast forward three months.
New laptop, new Windows 7 install (should be basically the same, but who really knows?), new VM, etc.
Guess what's running now?
Thumps along just fine. I know the bug is still there, I'm not that dellusional.
Charlie Gilley
<italic>You're going to tell me what I want to know, or I'm going to beat you to death in your own house.
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
First off, I'm still new to coding in C++ and so I hope this is the right forum for my question...
I'm coding a very small utility for a niche need we've got where I work. Right now I've got things coded so that it uses CreateProcess to fire off another program we run. What I need to do is then have this utility stay open and running (the program that it kicks off can terminate the original program once it's done doing its thing - and I can't just wait for a return code because the program that gets kicked off may kick off others, and one of those might be the final item to run).
Right now it launches our program and then, since it's done doing all the code, it finishes up and quits. Is there any way to tell the thing to stay open and running? Kind of like a service (but not a service because I don't want to install anything).
|
|
|
|
|
Just add a loop with a timer, and whatever other functions you need. The sequence would then be something like:
while TRUE
if applicationFlag is TRUE
then
start external application
set applicationFlag FALSE
end if
sleep for some seconds
end while
You will need to add some other decision code to set the applicationFlag to TRUE at some point.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
What if the program remains open for an hour or three (I hope they close it sooner, but I have to think of the "what ifs")? Is the Sleep command going to be the right way to go, or am I going to create a problem when using it over an extended period of time?
|
|
|
|
|
The other alternative is to use a timer: Timers Tutorial[^] is a great article which shows how to implement a few.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
If I'm understanding correctly (I'm not certain I am, your description is anything but clear) something like this might help (you can wait on a process handle):
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
BOOL bOk = CreateProcess(
_T("C:\\Windows\\system32\\notepad.exe"),
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi
);
if (bOk)
{
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
MessageBox(NULL, _T("Finished"), _T("Create and wait"), MB_OK);
}
Steve
|
|
|
|
|
I just downloaded the USBView sample application...
http://code.msdn.microsoft.com/windowshardware/USBView-sample-application-e3241039
...and I'm getting a cryptic error when compiling with Visual Studio 2010.
1>------ Rebuild All started: Project: usbview, Configuration: Win8 Debug Win32 ------
1>Build started 10/5/2012 11:58:11 AM.
1>C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.Targets(518,5):
error MSB8008: Specified platform toolset
(WindowsApplicationForDrivers8.0) is not installed or invalid. Please
make sure that a supported PlatformToolset value is selected.
1>
1>Build FAILED.
1> 1>Time Elapsed 00:00:00.16
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Any thoughts or ideas to resolve this would be appreciated. I'm using Windows 7, 32-bit. Thanks.
|
|
|
|
|
|
Thanks, Wes. I was able to resolve the problem by installing Visual Studio 2012 Express and recommended components, removing the offending line from the Microsoft.Cpp.Platform.targets file (double-clicked error message), and compiling as Release.
|
|
|
|
|
Hello,
I have the multimap :
TranslationTypeMap m_TranslationsMap;
where
typedef std::multimap< std::pair<CString, CString>, structTranslationData> TranslationTypeMap;
and
struct structTranslationData
{
std::wstring wstrTranslationId;
std::wstring wstrTextTranslated;
};
here an example of insertion in this map:
structTranslationData pstTranslationData;
pstTranslationData.wstrTranslationId = xxxx; pstTranslationData.wstrTextTranslated = xxxx;
strIdCaption = yyyy;
strIdLang = zzzz; m_TranslationsMap.insert(std::make_pair( std::make_pair(strIdCaption, strIdLang), pstTranslationData ) );
And finaly I write the code below to free memory used by this multimap:
m_TranslationsMap.erase(m_TranslationsMap.begin(), m_TranslationsMap.end());
can we say that memory is free ?
if not , what is the good way to do this ?
Thank you
|
|
|
|
|
It might depend on the implementation of the multimap. I checked it long ago but if I remember right your erase() call sets back the allocation to a minimum, identical to the allocation performed by the default ctor of multimap (at least in the SGI stl implementation of Visual C++ I used at the time). Note that even newly created empty std containers have a small piece of memory preallocated and some containers (like std::vector) dont shrink the size of the allocated memory area (capacity) even if you erase items.
A trick that seems to reset the allocated memory of any std containers regardless of stl implementation and container type is the following:
typedef std::multimap<int,int> MyMap;
MyMap global_map;
void my_func()
{
MyMap empty_map;
empty_map.swap(global_map);
}
|
|
|
|
|
Why mix std::string s and MFC CString s? And yes, the memory used to store the map's contents is freed since the map will handle destructing its contents and the contents don't require any manual cleanup.
Steve
modified 12-Oct-12 4:05am.
|
|
|
|
|
Well, I didn't assume the question is about freeing memory on destruction because that is trivial, however it might be the OP was curious about that... 
|
|
|
|
|
Hello Dear,
I created a sample Win32 DLL .Then i import COM component using "#import ...\XYZ.tlb" .
Now i able to access all the interfaces belongs the component . But i unable to access any methods belong to that interface.
Kindly guide me . If anything i'm missing the steps to access Interface -methods using "#import .tlb"
Thanks in advance 
|
|
|
|
|
You're probably missing out on the namespace.
You can either provide the using clause - using namespace <namespace>
Or specify that you do not want to use namespaces - #import ...\XPZ.tlb no_namespace
|
|
|
|
|
Welcome back, we missed you.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Thanks Richard. Been busy.
By the way, you're doing a heck of a job here.
|
|
|
|
|
I'd be doing a better one if I could see more of your answers.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi,
I have created a ATL toolbar using CreateWindow() API and attached ToolButtons using
AddButtons() API.
Now I want to add tooltips to these buttons.
How to add tooltips to the ATL toolbar.
|
|
|
|
|
Windows 7, Visual Studio 2008, MFC, C++
Intellisense says the arguments for this method are:
Send( const void *lpBuf, int nBufLen, int Flasg = 0 )
An MSDN web page has example code that looks like this:
Send((LPCTSTR)m_sendBuffer + m_nBytesSent,
m_nBytesBufferSize - m_nBytesSent);
Either way, I cannot get the right combination. I have:
SYSTEMTIME m_current_time;
GetSystemTime( &m_current_time );
int t1 = sizeof( m_current_time );
int chars_sent = m_C_Server_Send_Time_Socket->Send( (LPCTSTR) m_current_time, t1, 0 );
Where m_C_Server_Send_Time_Socket is the object created from CAsyncSocket to do all the transactions once the connection has been established.
How should the arguments to Send() be written?
Thanks for your time
modified 18-Sep-12 22:50pm.
|
|
|
|
|
bkelly13 wrote: How should the arguments to Send() be written? As described in the documentation[^] is usually the right way. In your case why would you cast to LPCTSTR when it specifically states to cast to const void* ? So your code should look like:
int chars_sent = m_C_Server_Send_Time_Socket->Send((const void*)&m_current_time, t1, 0 );
Maybe a bit of a refresher about casts, pointers and the addressof operator would be useful.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I am sure I tried that. I probably had a typo and did not recognize it.
Thank you for taking the time to reply.
Thanks for your time
|
|
|
|
|
Here is the thing: In C/C++ every pointer is automatically casted into (const void*) and every non-const pointer is automatically casted into (void*) . The msdn web page used cast just because of the pointer arithmetic involved, as you see they add an integer value to the start of the buffer: (LPCTSTR)m_sendBuffer + m_nBytesSent . They cast the buffer pointer into LPCTSTR because they want the addition to step the pointer with *byte* granularity. The problem with this is that LPCTSTR can translate not only to (const char*) but also into (const wchar_t*) (when the project character set is unicode, and note that wchar_t is a type whose size is 2 bytes!) so I guess MS guys made a mistake here and (LPCTSTR) is a bug (if the project character setting is set the unicode), they should have used (char*) or its equivalent in winodws: LPSTR or const char* or LPCSTR or something like that, something that is a *byte* pointer. Your problem is that you try to convert an instance of your struct into a pointer. You can not convert an instance into a pointer! You can converty only a pointer into a different type of pointer and in rare cases conversion might be needed between pointer and integral types.
SYSTEMTIME m_current_time;
GetSystemTime( &m_current_time );
int t1 = sizeof( m_current_time );
int chars_sent = m_C_Server_Send_Time_Socket->Send( (LPCTSTR) m_current_time, t1, 0 );
int chars_sent = m_C_Server_Send_Time_Socket->Send( (LPCTSTR) &m_current_time, t1, 0 );
int chars_sent = m_C_Server_Send_Time_Socket->Send( &m_current_time, t1, 0 );
Note that using (LPCTSTR) is a bug even on microsoft's side! You need to convert your struct pointer into a (char*) only if you want to step your pointer with byte precision!
SYSTEMTIME m_current_time;
SYSTEMTIME* p = &m_current_time + 1;
SYSTEMTIME* p = &(&m_current_time)[1];
char* p = (char*)&m_current_time + 1;
void* p = (char*)&m_current_time + 1;
const void* p = (char*)&m_current_time + 1;
Note that an addition or a substraction on a pointer always steps the pointer with the size of the type the pointer points to (like when you indexing into an array of the specified type). For this reason you can not step void pointers without casting them into something else - the size of void isn't defined.
|
|
|
|
|
But using LPCTSTR ensures that the pointer is incremented by the number of characters (whether Unicode or ASCII) not the number of bytes, so Microsoft are doing it right.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
If we speak of unicode and wchar_t then its not guaranteed that the data is transferred per character over the network, the variable name they use for incrmenting also reflects this: m_nBytesSent . They code heavily relies on the fact that LPCTSTR==LPCSTR in their case. Changing to unicode charset would introduce a hidden bug that compiles silently. If you decide to write code that has to compile with both ansi and widechar setting then using LPCTSTR is valid in many cases but this is an exception. I myself question the usefulness of supporting both ansi and widechar these days (so I don't anymore use defines like LPCTSTR and LPSTR) since its pain in the ass to write a program that compiles with both settings and today we can say that the majority of machines runs NT whose native is utf16. Its also a pain to search for bugs that arise only with one of the settings.
|
|
|
|