How to change a variable value on conditional breakpoint in visual studio 2015 - c++

Is there any way to change value of variable to on a conditional breakpoint and continue execution.
My code is like this
switch(var){ //conditional breakpoint on this line
case 1:
break;
...
}
I put conditional breakpoint like below
(var == 0 ) || (var ==1) is true
So when this breakpoint hits, I want to change var = 2, and continue execution.
What I found: I found Action also, but it only log messages. Is there any way to executing a statement like var = 2 as Action taken on this conditional breakpoint.
I don't want to change code because building it takes hell lot of time.
Note: I am working C++ on Visual studio 2015

In Log a message to Output Window write {my_variable=12345} the side effect of log output is assigning 12345 to my_variable.
Take into account that Log a message can accept and evaluate complex expressions in curly braces.

You can modify a variable directly in memory to change the execution flow through a quick watch (Shift+F9) or watch window in Visual Studio.

Make sure that in Tools / Options / Debugging you have the "Enable Edit and Continue" Enabled/Checked and then you will be able to edit your code while debugging and continue without the need to rebuild or to stop execution.
More information can be found in How to: Enable and Disable Edit and Continue

This used to work, I can't seem to get it to work now. Worked excellent on loops and also good for altering values without altering the source or the code (and accidently committing it).
Conditional break point, break if true
(url = "http://localhost:1234/") != url
This works thanks to assignment always returns it's assigned value. This will never be true as url becomes localhost

Related

VS Code "step into" debugger configuration

When I debug my C++ project in VS Code and toggle a breakpoint, The "step into" option redirecting me to external files/libraries, but I want to step only into my code.
I tried to add "justMyCode": true option in launch.js file, but it says this property is not allowed.
What can I do to go only through source code?
One thing could be done for this issue. I actually tested my own test program:
std::vector<std::string> name;
std::cout << "Enter a name: ";
std::getline(std::cin, name);
nameList.push_back(name); // BREAKPOINT
When we select Step Into in the breakpoint execution, it redirects into the definition of the function where it's actually defined, it's not going to debug only for your file in this case.
Step Into Type:
Rather, you must use Step Over since it doesn't tries to find the function definitions and you would able to debug your content straightforwardly.
Step Over Type:
The Step Into will certainly redirect you into the function definition wherever it's defined, if you want to know the declaration and other things about the related function, it's good opportunity to use Step Into. But in case you just want to debug the line only and not considering to go in depth and don't want to get jumped into another file, you must use Step Over.
If you want to know more about Step Into and Step Over, you can go through this Stack Overflow thread. I've found this helpful too.
This is not a perfect answer,
Suppose you are debugging code this:
func_that_we_care ( func_that_we_dont_care( i) ); <== step into this line
and you fall into 'func_that_we_dont_care', you can 'step out' there, then 'step into' again, eventually you will be able to step into 'func_that_we_care'.

How to stop visual studio 2017 c++ from autocompleting ) while in the middle of FOR statement?

So every time I write something like
for (auto i = 0; i < my_vector.size();
the moment I add the semicolon after a function call in a for statement, it turns into
for (auto i = 0; i < my_vector.size());
and then I have to manually delete the automatic bracket close because I havent finished the for statement, super annoying.
I don't remember earlier VS versions doing this.
Anyway I tried going in options -> text editor -> c/c++ -> formatting -> general, I tried switching off "Automatically format statement when I type a ;" and also "Automatically format braces when they are automatically completed". Both had some minor effect but the problem I described was still happening.
I like autocomplete most of the time, I find it useful so I don't want to turn it off completely but how can I make it stop doing that one thing?
Update the Visual Studio. That particular (VS 2017) bug is now fixed starting with the VS 2017 15.4 Preview 2 update.

AutoHotKey if statement not working

What is wrong with this?
if A_OSVersion = WIN_7 or A_OSVersion = WIN_XP
{
MsgBox, Win7 or XP
} else {
if WinActive("ahk_exe Explorer") {
!f::!h
}
}
The goal is to replace Alt+F with Alt+H in Explorer when I'm running Windows 8, 8.1, or 10, and I am testing on a Windows 7 machine, so the code shouldn't run at all, but the hotkey does and the MsgBox doesn't. Do I need to reorder things for it to work? The syntax looks right to me, but I've been having a hard time with syntax for AHKscript.
Kind comments are appreciated!
This
!f::!h
isn't a command, it's a key remap and thus, implicitly contains a return. You cannot use it within executable context. For details, see http://ahkscript.org/docs/misc/Remap.htm#Remarks somewhere below.
You wouldn't wanna have a return somewhere in between the lines which should be executed, would you.
(this was mostly a copy of my answer here)
For context-sensitive hotkeys, use the preprocessor directives:
#if (A_OSVersion = "WIN_7" or A_OSVersion = "WIN_XP") and not winActive("ahk_exe Explorer")
!f::!h
#if
#if winActive("ahk_exe Explorer") and !(A_OSVersion = "WIN_7" or A_OSVersion = "WIN_XP")
!f::!h
#if
To do it within normal script flow you will need to used the Hotkey Command but then it will not be a remap but a hotkey that sends another set of keys...

ODBC, SQL_SUCCESS_WITH_INFO, Cursor type changed - happening on a Stored Procedure with a single SELECT

I'm wondering whether anyone else has had this problem and how they resolved it?
Our application makes the follwoing ODBC call:
CString strCmd = "sprTestSingleSelect";
rc = SQLExecDirect(hstmt, (UCHAR*)(LPCSTR)strCmd, SQL_NTS);
The call retruns SQL_SUCCESS_WITH_INFO. By checking SQLGetDiagRec we see the message; Cursor type changed.
We found the following articles from Micrsoft;
http://support.microsoft.com/kb/156500/en-us
http://msdn.microsoft.com/library/ms130807.aspx
The stored procedure, sprTestSingleSelect, was created specifically to test what both articles hinted at - multiple selects cause the change.
CREATE PROCEDURE sprTestSingleSelect
AS
BEGIN
SET NOCOUNT ON;
SELECT id, firstname, lastname FROM address
END
GO
However, even with this very simple (single SELECT) stored procedure, the cursor type is still being changed (from SQL_CURSOR_KEYSET_DRIVEN to SQL_CURSOR_FORWARD_ONLY).
We need to cursor type to stay at SQL_CURSOR_KEYSET_DRIVEN, as later on in the application we are calling SQLFetchScroll(hstmt, SQL_FETCH_LAST, 0);, which is falling because of the incorrect cursor type.
Does anyone have an idea of what we might be doing wrong or what is going wrong?
We are using MS SQL Server 2008R2
Our application is written in C++ (using Visual Studio 10 Premium)
We've managed to get the above test working, however the solution is not very satisfactory.
When the store procedure only contains a single SELECT statement and nothing else it works. So to get it working all we had to do was remove the statement SET NOCOUNT ON;. As a side note, we tested setting NOCOUNT to OFF which also didn't work - removing the statement alltogether is what was required.
This seems to make using stored procedures to return data (result sets) rather useless!
If anyone has another (better) solution, we'd love to hear it...

Visual Studio Breakpoint Macro to modify a value?

I'm debugging an application (C++), and I've found a point in the code where I want to change a value (via the debugger). So right now, I've got a breakpoint set, whereupon I do:
Debugger reaches breakpoint
I modify the variable I want to change
I hit F5 to continue running
lather, rinse, repeat
It's hitting this breakpoint a lot, so I would like to automate this. I would like to set the Breakpoint to run a macro, and continue execution.
However, I have no experience writing VisualStudio macros, so I don't know the commands for modifying a variable of the executing program. I've looked around, but haven't found anything helpful online so far.
I found how to do this with a macro. Initially, I tried using Ctrl-Shift-R to record a macro of keystrokes, but it stopped recording when I did Ctrl-Alt-Q. But I was able to edit the macro to get it to work. So here's what I did, in case anyone else wants to do something similar.
Tools -> Macros -> Macro Explorer
Right Click -> New macro
Public Module RecordingModule
Sub setvalue()
DTE.Debugger.ExecuteStatement("variable_name=0")
End Sub
End Module
This macro will execute the assignment statement, setting my variable (in this case, making it a NULL pointer).
Right Click on a BreakPoint -> When Hit...
Check "Run a macro"
Select Macros.MyMacros.RecordingModule.setvalue
Check "Continue execution"
Click OK
Then, I was able to run my program, automatically adjusting a pointer to NULL as it went. This was very useful for testing, and did not require recompiling.
Looking for similar today and found that you can also use the 'Print a message:' option instead of a macro. Values from code can be printed by placing them inside {}. The key is that VS will also evaluate the content as an expression - so {variable_name=0} should achieve the same as the macro example.
If you are think of a macro in the same way as Microsoft excel, then you're out of luck. It doesn't quite work that way.
In C++, a macro refers to a small inline function created with #define. It is a preprocessor, so a macro is like using a replace on all its references with its body.
For example:
#define add(a,b) ((a)+(b))
int main() {
int a=3, b=4, c=5, d=6, e, f;
d = add(a,b);
e = add(c,d);
}
Would like to the c++ compiler as:
int main() {
int a=3, b=4, c=5, ...;
d = ((a)+(b));
e = ((c)+(d));
}
Now, back to your question. If the variable is within scope at this breakpoint, just set it from within your code:
myVar = myValue;
If it is not, but it is guaranteed to exist, you may need a little hack. Say that this variable is an int, make a global int pointer. If this variable is static, make sure to set it to its address, and back to NULL inside it's scope. If it is dynamic, you may need some extra work. Here is an example:
int* globalIntPointer;
void func() {
*globalIntPointer = 3;
//...
}
int main() {
int a = 5;
globalIntPointer = &a;
func();
//...
globalIntPointer = NULL; // for safety sake
return 0;
}
You can execute a VS macro when a breakpoint is hit (open the breakpoints window, right click on the breakpoint in question, and select "When Hit..." off the popup menu). I'm less certain about writing a macro that modifies a variable of the program under debug though -- I've never done that, and a quick try with attempting to record a macro to do it doesn't seem to work (all it records is activating the right window, not changing the value).
Select "Condition..." and write an assignment for the variable in question in the "Condition:" textbox. This will naturally resolve to "true" with it not being an actual conditional test. Therefore, the breakpoint is never hit, and your variable has been set accordingly.