OutputDebugString capture - winapi

In a previous question I asked about if any real-time enhanced versions of dbgview exist,
and ended trying to write my own, which worked out well except for one small snag
OpenMutex(MUTEX_ALL_ACCESS, FALSE, "DBWinMutex")
returns a handle to this mutex, except it returns NULL on windows2003 , anyone know why
this might be the case ?

The mutex doesn't necessarily exist. e.g. OutputDebugString attempts to create it, rather than open it.
Details here: http://www.unixwiz.net/techtips/outputdebugstring.html

Just in case, I faced similar permission issue when trying to use open mutex in MEX file.
This worked for me:
auto str = TEXT("MutexTest");
HANDLE h1 = OpenMutex(SYNCHRONIZE, FALSE, str);

Related

Python: Detect code which gets never executed in production

I need to do refactoring in a big legacy Python code base.
Often I think "these lines don't get executed in production any more".
But I am unsure.
There are some tests which touch these lines. But I can't tell for sure if really no usage happens in production.
What can I do in this situation?
This question is about coverage on a production system. This question is not about coverage during testing/CI.
I don't want to comment out that lines, since I don't want to produce an error in the production system.
Common practice is to use logging inside that lines of code. e.g. you have a block of code you think is not in use. You add try catch block in the beginning of that block of code. Inside trycatch you add line to a specific json named same as your suspicious block of code.
try:
with open("block1.dat", "rb") as file:
activity = pickle.load(file)
curtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
currentact = "dt = {}; code done that: var1 = {},
var2 = {}".format(curdate, var1, var2)
activity.append(currentact)
file = open("block1.dat", "ab")
pickle.dump(activity, file)
file.close()
except Exception: pass
You can use telegram api to log code to. After a while You'll get info how often your code works and what does it do.
Then you monitor for a while and if nothing happens in a month, You can comment the block.
Is the production system deterministic?
Is it interactive?
Does control flow depend on input data?
Do you have access to all possible inputs?
Do the tests exist for a reason or just because?
I'd be careful removing code based on what is needed based on logging unless I knew there are no exceptional situations that occur rarely.
I would follow the common code paths to try to understand the codebase piece by piece in order to figure out what can be simplified. It's hard to give more specific advice without knowing more about the system you're dealing with.
We use a simple pattern to handle this: looks_like_dead_code(my_string)
This is a method which logs the string "my_string".
Example usage:
if ext == '.jpe':
looks_like_dead_code('2018-11-30 tguettler: looks fixed in mime_type_to_extension')
Using the date and the developer login is not enforced, it is just best practice.
If the line gets executed the one who is responsible for checking the logs will talk to the developer.
Since our production environments get updated roughly once in two weeks, you can be sure that this line was not executed during the last months.
I like this solution since in most cases it is like this:
you want to fix a bug or implement a new feature
you look at the code and see some lines which look like dead code. I mean code which is useless, since it won't get executed any more.
You don't have hours of time to investigate. You can dive into your vague guess that this is dead code. You want to do your actual work (fix a bug or implement a new feature. See Step1)
The method looks_like_dead_code() gives you a way to actually do something and leave a note for other developers. It only costs some seconds improve the current situation.
If you have a Tickler file System you can remind yourself to check this code in six months. At least in my context I can be very sure that this is dead code if this line was not executed for several months.

Create Symbolic Link on Windows using C++

I would like to create symbolic links to my data from my program in a cross-platform manner. For *nix systems, there is the symlink. For Windows, I found the function CreateSymbolicLink from this answer in SO and I am running it in this way:
int test = CreateSymbolicLink(input_fileName.c_str(), ouput_fileName.c_str(), 0);
But test always returns 0, which means that the function failed (and the file specified in output_fileName is indeed non-existent). What am I doing wrong?
Thanks in advance!
UPDATE:
I did run GetLastError right after the above call:
DWORD err = GetLastError();
with err = 1314. Thanks #David
UPDATE 2:
According to #David's reply, Windows does not let non-admins create soft-links. I am keeping the question open if a future iteration of Win API changes this. Then I will update the answer. As of 26/March/2015, #David's answer is correct.
From the documentation:
If the function fails, the return value is zero. To get extended error information, call GetLastError.
You don't appear to have called GetLastError. You should do so. A likely error code is ERROR_PRIVILEGE_NOT_HELD which has value 1314. You'll get this if the calling process does not have enough rights. You need to be running as an elevated administrator to use this function.
use c++17 create_symlin instead https://en.cppreference.com/w/cpp/filesystem/create_symlink
It works fine.
I grabbed the USB stick that was formatted as FAT for my test. the above code should work if you are using the correct volume format and you are in the Administrator Group. If you are in Windows 10 you should not need to Adjust Token Privileges, and you do not need UNC for the parameters.

sigabrt on filteredArrayUsingPredicate

I'm a noob and trying to convert an example from a book into an app I can use.
The sample app is a modified version of the contacts application and it works.
I've done some further modification, and the search no longer works. It sigabrts on the following line
self.filteredAnswercards = [flattenedArray
filteredArrayUsingPredicate:predicate];
I'm stumped.
my head is bloody from beating it against my keyboard.
ANY help is massively appreciated.
Thanks.
My suggestion was to wrap the line that crashes inside a #try/#catch block and, inside the catch, log the exception and the result of the exception's callStackSymbols method.
For the record, part of the problem with the 4.x versions of Xcode is that they are much worse than 3.x versions at telling you where an exception is coming from. For this reason, getting familiar with tricks that make a program or the debugger tell you what you need to know is very important.
I would guess that predicate is nil. Where did you assign it? Or did you never assign it?
Very hard to say without seeing more code. Sigabort generally means an exception has been thrown. You can put a break point in objc_exception_throwto get a back trace which should help highlight the cause.
If you get NO new info with the above, others have said that a full reboot of the computer can help... but I have not had a situation where I could verify this.
---- edit based on comments -----
It sounds like filteredAnswercards is nil, that will definitely cause a Sigabort. Allocate that array properly and you should be good to go.

glGenBuffers is NULL giving a 0x0000000 access violation when using glew

> I have visual studio c++ express and a NVIDIA GeForce 7900 GS. I'm using glew to get at the openGL extensions. Calling glGenBuffers crashes as its a NULL pointer tho. I have an open GL context before I make the call ( wglGetCurrentContext() != NULL ). I'm calling glewInit() before the call. glewGetString( GLEW_VERSION ) is returning GLEW_VERSION_1_5. What am I doing wrong ? Is the card too old ? Is it the driver ?
Remember to make a glewInit() call in your code so that you get valid pointers to GL functions.
Hope it helps.
Without seeing your code it would be difficult to tell, but what you are attempting to do seems like it could be helped a lot by using GLee. It is designed to load all current extensions and you have the ability to check what is supported, e.g. :
#include <gl\GLee.h> // (no need to link to gl.h)
...
if (GLEE_ARB_multitexture) //is multitexture support available?
{
glMultiTexCoord2fARB(...); //safe to use multitexture
}
else
{
//fallback
}
The above was shamelessly copy/pasted from the GLee site, but it displays the functionality I'm trying to showcase.
You need to call glewInit() post having a valid context. And that would be, in the world of glew, after you've called glfwMakeContextCurrent(myWindow);
I have actually run into this problem with GLEW. For me, it was nullifying the function pointer for glGenerateMipmap. I fixed it by simply restoring the pointer to the appropriate function. This is my example in Linux:
glGenerateMipmap = (void(*)(GLenum))
glXGetProcAddressARB((GLubyte*)"glGenerateMipmap");
There is a WGL equivalent for glXGetProcAddress; I just don't remember the name off the top of my head. Try manually restoring the functions using this method. If you come across many functions that are null, something is definitely wrong in your setup process. The only other functions I recall having to restore were glGenVertexArrays, glBindVertexArray, and glDeleteVertexArrays. If your glGenBuffers is null, odds are that glBindBuffer and glDeleteBuffers are null as well. :(
Test if the desired extension is actually supported by checking the string returned by glGetString(GL_EXTENSIONS); if it's not there you know what's causing your problems.

WinAPI CIniFile::ReadFile() can't read ini file

IDE: VS2005
Is there anyway to know why ReadFile() failed? I can't find the reason some of the INI can't be read. Thanks.
EDIT:
CIniFile iniFile;
iniFile.SetPath( "C:\\Services\\Server\\Server.INI" );
if( iniFile.ReadFile())
my code...
The program never gets in the if block.
And, sorry for the confusing. I use this library for the CIniFile class. Hope this information helps to pinpoint the problem.
http://www.codeproject.com/kb/cpp/cinifileByCabadam.aspx
EDIT2: I found the reason, it's because some of the ini files are saved as Unicode. And that's the reason ReadFile() fails. But now the question is how to read Unicode ini files.
Normally GetLastError() should give you an error number to look up
EDIT: In the CIniFile project there seems to be no default constructor, try instead CIniFile( string const iniPath ) i.e.
CIniFile iniFile( "C:\\Services\\Server\\Server.INI" );
if( iniFile.ReadFile())
EDIT2: OK, you would need to modify the code to instead of using fstream use wfstream - see

Resources