I'm starting to code with vb.net and i need to run the code directly, like it happens with java: in the cmd i can run the class files. Is there any similar possibility with vb.net, preferably directly from the visual studio hub?
Thanks!
You can use the Immediate Window for that. It offers many different ways to interact with your code. To use it, start your application in debug mode from Visual Studio and press CTRL + ALT + I.
To execute a shared method you can type in the Immediate Window:
className.methodName()
(example)
MainFunctions.DoStuff()
DoMoreStuff()
Where className is optional if you're currently already inside the class (for example if you've hit a breakpoint in it).
If you want to execute an instance (non-shared) method you can either use the method above (without className, but you must currently be inside the class by hitting a breakpoint, for example), or you create a new instance of the class and execute the method:
Public Class MiscFunctions
Public Sub PrintHelloWorld()
Debug.WriteLine("Hello World!")
End Sub
End Class
(Immediate Window)
New MiscFunctions().PrintHelloWorld()
Hello World!
Dim m As New MiscFunctions
m.PrintHelloWorld()
Hello World!
You can also print the value of a variable or the return value of a function by typing:
? variableOrFunctionNameHere
(example)
? ImageCount
4
The same rules for executing methods applies to evaluating functions too:
Public Class MiscFunctions
Public Shared Function IsEven(ByVal Num As Integer) As Boolean
Return Num Mod 2 = 0
End Function
Public Function Sum(ByVal a As Integer, ByVal b As Integer) As Integer 'Non-shared method.
Return a + b
End Function
End Class
(Immediate Window)
? MiscFunctions.IsEven(3)
False
? MiscFunctions.IsEven(8)
True
? New MiscFunctions().Sum(3, 9)
12
You can also dynamically evaluate expressions:
? ImageCount + 1 = 5 'Here, ImageCount is 4
True
? 2 * 4
8
if you are looking for more then the Immediate Window. Look into Microsoft CodeDom. I have used it to compile C#, VB.net, C++, and Visual J#.
https://msdn.microsoft.com/en-us/library/y2k85ax6(v=vs.110).aspx
Related
I am using ConfuserEx to obfuscate my program before release, I want the program to show a warning if it is being run without obfuscation. So as to reduce the chance of a non obfuscated executable being shipped.
So at runtime I want a function which returns true/false depending if obfuscation has been applied.
I can see two ways of doing it.
If the obfuscation process you use is part of the release build and therefore your release build has embedded instructions for the obfuscation part, you could do something like that.
AppValidator.Validate()
The validation will validate it is a release version or if not, that the application is run for allowed users (dev. team for instance).
I also added a way to validate by commandline calling Myapp.exe validate
However. this does not validated obfuscation per see, it validates the application is in Release mode.
Your obfuscator, if embedded with the release build, should fails if he cannot obfuscate release version or the premise for this validation is not good.
Public Class AppValidator
#If DEBUG Then
Private Shared ReadOnly IsDebugVersion As Boolean = True
#Else
private Shared ReadOnly IsDebugVersion As Boolean = False
#End If
Private Shared ReadOnly ISValidUser As Boolean = ValidateDevUser()
''' <summary>
''' Validate the user is authorized to run the program as
''' </summary>
''' <returns></returns>
Private Shared Function ValidateDevUser() As Boolean
Try
' Custom validation to determine if used in dev. environment such
'as validating username and domain name or checking agains Dev. Registry key
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Shared Function Validate() As Boolean
Dim Args = Environment.GetCommandLineArgs
Dim ConsoleValidate As Boolean = Args.Count = 2 AndAlso String.Compare(Args(1), "validate") = 0
If IsDebugVersion Then
If ConsoleValidate Then
Console.WriteLine(Not IsDebugVersion)
Application.Current.Shutdown()
Return False
End If
If Not ValidateDevUser() Then
MessageBox.Show("Access Denied")
Application.Current.Shutdown()
Return False
End If
End If
Return True
End Function
End Class
The first solution is best if you can be sure that build will fails if release version produced and obfuscator steps fails.
If you cannot be sure of that, you can maybe take a look at
Obfuscation checker, from Red-gate, which is free and has a command-line and do exactly what you seek in a direct approach.
You could use something like ILSpy and have it run automatically after build with WM_COPYDATA API arguments, like so:
https://github.com/icsharpcode/ILSpy/blob/master/doc/Command%20Line.txt
Have it navigate to a type name that should be obfuscated; if it fails, you know obfuscation succeeded.
I am working on a little framework and I want to have a class file that contains functions I can run to check if a certain key has been pressed so other events can be run. The only code I have found online for similar things are written into the form itself and use something like "Handles Me.KeyPress". However, this handle function can't be used in a class file.
Public Function OnKeyPress(KeyToCheck As Keys)
If KeyPressed = KeyToCheck then
return true
else
return false
End If
End Function
I have tried:
Public Function OnKeyPress(KeyToCheck As Keys)Handles Formname.Keypress
If KeyPressed = KeyToCheck then
return true
else
return false
End If
End Function
However, this does not work. Any suggestions or work arounds would be greatly appreciated.
To get keyboard input, you need to have a window. All input goes to a window, and that window can then check for key presses that are sent to it.
To get global information, you'd need to install a hook, but those should generally be avoided.
I have a module that looks like this. I've simplified it greatly so as not to clutter up my question (my local functions are more complex than this). Here is my code:
decision = {}
function win(win_arg) return win_arg end
function lose(lose_arg) return lose_arg end
local function draw(draw_arg) return draw_arg end
function decision.get_decision(func, arg)
return func(arg)
end
return decision
I am calling my module with the code below.
my = require "my-lua-script"
print(my.get_decision(lose, "I lose."))
print(my.get_decision(win, "I win."))
I want 'get_decision' to be a public method. I want win, lose and draw to be private, but I want to call them dynamically from get_decision. If I understand correctly, win and lose are in the global namespace right now? If I put a local in front of these two methods (like draw) then my code doesn't work.
Is there a way to accomplish what I am after?
Of course.
my-script.lua
-- This is the local side of the module.
local outcomes = {}
function outcomes.win(arg) return arg end
function outcomes.lose(arg) return arg end
function outcomes.draw(arg) return arg end
-- This is the exposed side of the module.
local M = {}
function M.get_decision(outcome, arg)
return outcomes[outcome](arg)
end
return M
main.lua
local my = require'my-script'
print(my.get_decision('win', 'I win.'))
print(my.get_decision('lose', 'I lose.'))
You simply use a string to indicate which function you'd like to access, and use that string to index a table of functions from within get_decision against outcomes. This will keep the functions hidden behind get_decision.
I have a class file myClass.m in a package folder +myPack that's on the path. A simple example of the class file is:
classdef myClass
properties
prop
end
methods
function obj = myClass(x)
obj.prop = x;
end
end
end
Now if I directly call the method and access the property using the full package name, i.e.:
x = myPack.myClass(2).prop;
returns x = 2 correctly. Now, if I try the same by importing this class (and not use the package name):
import myPack.myClass
y = myClass(2).prop
it gives me the following error:
Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.
Why does this work in the first case and not the second? As far as I understood, importing a class mainly allowed one to use the class name without the long package name (among other considerations). What is the difference in these two that causes this error and how can I work around it?
Here is some more weird for you: the behavior is different if you are running in the command window, from a script, or from a function!
1) command prompt (1st: ok, 2nd: error)
This is what you've already shown
>> x = myPack.myClass(2).prop
x =
2
>> import myPack.myClass; y = myClass(2).prop
Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.
2) Script (1st: error, 2nd: error)
testMyClassScript.m
x = myPack.myClass(2).prop
import myPack.myClass; y = myClass(2).prop
and
>> testMyClassScript
Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.
Error in testMyClassScript (line 1)
x = myPack.myClass(2).prop
(the second line would also throw the same error)
3) Function (1st: ok, 2nd: ok)
testMyClassFunction.m
function testMyClassFunction()
x = myPack.myClass(2).prop
import myPack.myClass; y = myClass(2).prop
end
and
>> testMyClassFunction
x =
2
y =
2
I would definitely call that a bug :) The expected behavior is to give an error in all cases.
Here is my scenario.
I have the following line of code in my program:
JCL_History.Enqueue(JCL_History(I))
This JCL_History object is basically a Generic.List encapsulated in a wrapper and has the following method:
Public Sub Enqueue(ByRef value As String)
If Members.Contains(value) Then
Me.RemoveAt(Members.IndexOf(value))
ElseIf _Count = _MaxCount Then
Me.RemoveAt(_Count - 1)
End If
Me.Insert(0, value)
End Sub
So you see that the first line of code that invokes Enqueue should "shuffle" items around.
Also, the wrapper class of which JCL_History is a type has the following default property:
Default Public Property Item(ByVal Index As Integer) As String 'Implements Generic.IList(Of String).Item
Get
If Index < _MaxCount Then
Return Members(Index)
Else
Throw New IndexOutOfRangeException("Bug encountered while accessing job command file history, please send an error report.")
End If
End Get
Set(ByVal value As String)
If Index < _MaxCount Then
If Index = _Count Then _Count = _Count + 1
Members(Index) = value
Else
Throw New IndexOutOfRangeException("Bug encountered while accessing job command file history, please send an error report.")
End If
End Set
End Property
In my testing I have 2 items in this JCL_History list. When I call that first line of code I posted (the one that invokes Enqueue) with I = 1 I expect the first item to be shuffled to the bottom and the second item to be shuffled to the top.
After the thread returns from Enqueue I notice that this is exactly what happens to my list, HOWEVER if I hit the "step_in" button after the execution of Enqueue I go into the Default Property's set method where Index = 1 and value = and it screws everything up, because the item that got shuffled to the end (index 1) gets overwritten by the item value shuffled to the top.
So basically the set method on the default property is getting called at what I think to be a completely ridiculous time. What gives? By the way I'm running VS2005 on XP.
Do you have a watch and/or are you executing a quick watch via a mouse-over that would read a property that might invoke the Set? If your debugging environment evaluates something that changes data, you can get weird behavior like what you're describing.
Whether or not the case above is true, if multiple threads can write to Members, consider using ReaderWriterLockSlim.
No answer. Never fixed this, just worked around it.