I would like to know how to loop a function to open an executable program multiple times. I tried just putting system(path) and CreateProcess(Lpath, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) in a while, do while, and for loop, but that only opens the program once.
Here's what the relevant code looks like right now:
for(int i=0; i<10; i++)
{
CreateProcess(L"C:\\Users\\Ben\\Documents\\Visual Studio 2010\\Projects\\RANDWritter\\Debug\\RANDWritter.exe", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}
Anyone know what to do?
Before checking with GetLastError, it's always wise to see if the function you called returned an error code or not. If it just returned a success code, then the value you get from GetLastError might be bogus. So do also use BOOL createdOK = CreateProcess(...), and check for the returned value being != FALSE before calling GetLastError.
But if it's genuinely error 87 you get, then that explains: it's name is ERROR_INVALID_PARAMETER (you can look them up at MSDN's System Error Codes page). Looking at the MSDN docs for CreateProcess, you probably cannot omit the lpStartupInfo and lpProcessInformation parameters. It's also just a good idea to fill these in regardless, since they give you info on the newly created process. More so since the handles returned in the ProcessInformation must also be CloseHandle'd manually after you're done with them.
Related
I'm trying to load several symbol modules using the following code:
DWORD64 dwBaseDllSymLocal = 0;
SymInitializeW(GetCurrentProcess(), NULL, FALSE);
SymSetOptions(SYMOPT_DEBUG);
dwBaseDllSymLocal = SymLoadModuleExW(GetCurrentProcess(), NULL, L"C:\\module1.dll", NULL, 0, 0, NULL, 0);
if (0 == dwBaseDllSymLocal)
{
__debugbreak();
}
dwBaseDllSymLocal is 10000000 now.
dwBaseDllSymLocal = SymLoadModuleExW(GetCurrentProcess(), NULL, L"C:\\module2.dll", NULL, 0, 0, NULL, 0);
if (0 == dwBaseDllSymLocal)
{
__debugbreak();
}
Dbghelp gives the following message: module1 is already loaded at 10000000.
Same behavior happens when I try to load the same module twice. (unlike what is written in the documentation of the function).
Last error is ERROR_INVALID_ADDRESS though it doesn't seem relevant, because last error has this value following the first successful function call too.
Is it possible to load several modules with SymLoadModuleExW? What is the right way to do so?
You are loading these binaries outside of the context of a debugger session, right? In which case, the fifth parameter, BaseOfDll, might be causing a problem:
The load address of the module. If the value is zero, the library obtains the load address from the symbol file.
When loading a binary standalone, it might just use 10000000 for everything... in which case, the second module load would conflict with the first one. So try passing something different there.
Last error is [...] though it doesn't seem relevant, because last error has this value following the first successful function call too.
If the function succeeds, the last error is not applicable; it could contain anything, but you should ignore it unless the documentation explicitly says that it sets the last error in success cases.
I have setup a message loop.
I call SetTimer like this:
SetTimer(null, 5, 1000, timerFunc_c);
The return value of this is a random number like 11422. And it never triggers my callback. If i set timer like this:
SetTimer(msgWinHwnd, 5, 1000, timerFunc_c);
Then it returns 0 and it then makes GetMessage with 0 for min and max, trip with WM_TIME message, however my callback is never called.
Do you know why in first situation my callback does not return the id i told it? And why it never fires the callback?
Thanks
This is documented behaviour for the SetTimer function:
nIDEvent [in]
Type: UINT_PTR
A nonzero timer identifier. If the hWnd
parameter is NULL, and the nIDEvent does not match an existing timer
then it is ignored and a new timer ID is generated
If your callback isn't ever called (it's hard to tell for sure from your question), check your GetMessage loop and make sure you're not specifying a window filter (e.g. you should be calling GetMessage(&msg, 0, ...); rather than GetMessage(&msg, msgWinHwnd, ...);.
I was looking at the LSSharedFileListCreate function in LSSharedFileListQuestion and it's defined as:
LSSharedFileListCreate(CFAllocatorRef inAllocator, CFStringRef inListType,CFTypeRef listOptions)
inAllocator and listOptions are always set as null. I was wondering if there were any other valid values for them besides null.
LSSharedFileListCreate follows the same conventions as other Core Foundation functions.
The CFAllocatorRef you provide can be kCFAllocatorDefault, kCFAllocatorSystemDefault, kCFAllocatorMalloc, kCFAllocatorMallocZone, kCFAllocatorNull, or kCFAllocatorUseContext.
The one you want to use in most cases is kCFAllocatorDefault, which is synonymous with NULL, as explained in the interface file.
The value you provide to listOptions depends entirely on which value you provide; some have associated values, and some do not.
For example, if you provided kLSSharedFileListVolumesComputerVisible in inListType, you'd provide a CFBoolean value indicating TRUE or FALSE in listOptions. If you provided the example kLSSharedFileListSessionLoginItems, you'd provide NULL, since there's no associated value.
I want to turn off a code contract warning, but only for specific code lines. How do I do that?
For instance, I get:
Warning 87 CodeContracts: requires unproven: key != null
for:
return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
which will never happen in our applications.
Well, one option would be:
string key = typeof(T).AssemblyQualifiedName;
Contract.Assume(key != null);
return HttpContext.Current.Items[key];
It's a bit ugly, but I believe it should work.
As with most "legacy" MSDN pages, the page for ReportEvent has too little information for me to make much sense of it. I've tried searching, but can't find a good, clean, simple example of the function's usage. Could anyone suggest one?
I ended up using this:
HANDLE eventLog;
WORD type;
const char* msg;
// ... snip ...
ReportEvent(eventLog, type, 0, 1, NULL, 1, 0, &LPCTSTR(msg), NULL);
Seems to work well enough.
Well this seems to be a very old thread, landed here looking for a good example of Report Event... but figured out you have not received any replies...and would have probably already found the solution.
The reason you see "Event Id not found" is because the EventViewer is not able to load/lookup the text resource to be displayed for the event Id. Sorry if last line sounded geeky.. but this is what i understand of EventLog:
-EventLogging has two aspects
Registering with EventLog (or in other terms creating EventSource)
Logging or Writing into Event Log
Viewing or Reading from log
When you register in event log, you simply specify a eventSource (any name that identifies that log) + EventMessageFile, Category File and SupportedEventTypes. Here EventMessageFile points to a DLL/EXE that contains your message descriptions/resources.
When you log an event, you simply log it with some data like EventID, Category ID and EventData. But when you view it using any EventViewer (or Windows eventVwr.exe) the viewer reads your events, looks for a DLL/EXE associated with your eventSource(pointed by EventMessageFile), and renders the decription from the resource section of that DLL/EXE.
This DLL is nothing but a simple resource file that was compiled using MessageCompiler, and contains a "MessageTable". This is done to provide culture specific event logging
This is the reason, When you export the log into XML/TXT etc from your EventViewer, It asks you if you want to Save it "with Display informaion" or "without display information", so that you can view it on computers that do not have EventMessageFile.
JFYI the reg entry is located at:
HKLM\CurrentControlSet\System\Services\EventLog\Application
one catch: If you're wondering how .Net does it..., it simply does it by providing a default EventMessageFile called EventLogMessage.dll (found under %SYSTEMROOT%\Microsoft.Net\Framework\vXXXX\)
As I recall it is a pain to set up correctly - you need to add messages to you application using the Message Compiler - if you skip this you won't see useful messages only error codes. Take a look at Creating a Windows NT Service by Using ATL for an example
The sample Windows Service C++, is a windows service reporting to event log, you can get the code from https://code.msdn.microsoft.com/windowsapps/CppWindowsService-cacf4948
in particular, the following function (quoted from ServiceBase.cpp) does it
//
// FUNCTION: CServiceBase::WriteEventLogEntry(PWSTR, WORD)
//
// PURPOSE: Log a message to the Application event log.
//
// PARAMETERS:
// * pszMessage - string message to be logged.
// * wType - the type of event to be logged. The parameter can be one of
// the following values.
//
// EVENTLOG_SUCCESS
// EVENTLOG_AUDIT_FAILURE
// EVENTLOG_AUDIT_SUCCESS
// EVENTLOG_ERROR_TYPE
// EVENTLOG_INFORMATION_TYPE
// EVENTLOG_WARNING_TYPE
//
void CServiceBase::WriteEventLogEntry(PWSTR pszMessage, WORD wType)
{
HANDLE hEventSource = NULL;
LPCWSTR lpszStrings[2] = { NULL, NULL };
hEventSource = RegisterEventSource(NULL, m_name);
if (hEventSource)
{
lpszStrings[0] = m_name;
lpszStrings[1] = pszMessage;
ReportEvent(hEventSource, // Event log handle
wType, // Event type
0, // Event category
0, // Event identifier
NULL, // No security identifier
2, // Size of lpszStrings array
0, // No binary data
lpszStrings, // Array of strings
NULL // No binary data
);
DeregisterEventSource(hEventSource);
}
}