I have a C++/CLI WinForms project and in which I can happily print out the command line arguments in main using the following code
int main(array<System::String ^> ^args)
{
for each(String^ arg in args) {
System::Diagnostics::Debug::WriteLine(arg);
}
}
Is there a built in function that allows me to access these arguments again during the program or do I need to save them myself?
I've seen this question, specifically the second answer that says you can do this in C#:
string[] args = Environment.GetCommandLineArgs();
But is there a way specifically in C++/CLI?
The second part of my question is available in C++/CLI, you can do it like so:
array<System::String ^> ^args = System::Environment::GetCommandLineArgs();
Related
I'm trying to write a simple system call on XV6 (documentation available here and Github here) in order to understand how they're implemented. I've used these steps
In syscall.c, declared extern int sys_hello(void) and added [SYS_hello] sys_hello into static int (*syscalls[])(void) array
In syscall.h, defined SYS_hello as call number 22
In user.h, declared the function prototype as int hello (void);
In usys.S, added SYSCALL(hello) to the macro
In sysproc.c, added the function sys_hello(void) at the bottom
int sys_hello(void)
{
cprintf ("Hello World System Call\n");
return 0;
}
Created hello.c which simply calls the hello() system call
Added hello.c to the Makefile and ran the code
It worked as expected.
Now, my question is that it seems that the array in syscall.c matches the indexes of the commands with the system call numbers in syscall.h file
However, if I move the hello position to the second spot in the syscall.c and let the system command number in syscall.h stay 22 the system command works as before. Where as, I expected that it'd break. Can you help me understand how the array syscall.c maps (if that's even the correct word) to the syscall.h system call number?
I'm fairly new to XV6 and C so please don't get mad at me if this question seems silly, I'm only trying to learn.
Here is the Github link to my fork of the XV6 repository if that helps: github.com/AdityaSingh/XV6
The array of syscalls is syscall.c makes use of the designated initialization syntax where you can specify at which index to assign the value.
static int (*syscalls[])(void) = {
[SYS_fork] sys_fork,
}
SYS_fork, defined as 1, specifies the index where to assign sys_fork. Thus the order of the elements doesn't matter using this syntax.
I am currently working on a combination software and Arduino project that has the following general structure:
User inputs a string of commands through a terminal program such as CoolTerm
Commands are sent to Arduino through USB Serial
First command is parsed, along with included arguments
Function associated with first command is executed
Second command is parsed, along with included arguments
Function associated with second command is executed
Etc. until all commands have been parsed and executed
So far, all of this works as I would expect it to. However, the project I am working on requires very precise timing, and having to parse each individual command creates a considerable amount of processing time (not sure if this is the right term) between each command execution.
For example, in a user input string that contains three commands, between the first command being parsed and the last command being executed, there is an additional 5.8 milliseconds of processing time from start to finish.
To be clear, all parts of my program are functional, including user input, string parsing, and function execution as described above. I need to improve on my existing code, not correct errors.
Ideally, I imagine that the program will parse each command, "set aside" the function that is associated with the command, and execute all commands sequentially once they have all been "set aside." This will shorten the processing time significantly by getting rid of the need to continue parsing commands between each function execution. I am not sure how to accomplish this, or if it is even possible.
To illustrate my ideas in very basic C++ pseudocode:
(assuming example user input is "A, B, C")
loop() {
// Example user input received: "A, B, C" corresponds to:
// functionA, functionB, functionC
String userInput = receiveInput();
// Parse user input
parse(userInput);
// Execute functions specified by user input
executeFunctions();
}
/*Parsing separates "A, B, C" to functionA, functionB, functionC
Functions are "set aside" to be executed sequentially,
the next beginning directly after the last ends*/
executeFunctions{
// Example functions to be executed
functionA();
functionB();
functionC();
}
Question:
I need a way to create a function based on user input, or based on another function. I have never heard of such a concept through the extensive research I have done, and I am not sure if it exists. If possible, this is the method I would like to use to proceed with my project, as I believe it will require the least amount of restructuring of my code.
Edit:
This project requires compatibility with Arduino hardware and the Arduino IDE. Standard C++ will not work.
You could use a Command Pattern.
Basically, make your parser to put a different command object for each user input into some sort of queue. You can use a basic function object for this:
struct Command {
virtual ~Command() {}
virtual void operator()(); // this will execute the command
};
class FirstCommand : public Command {
// some private data + constructor
public:
virtual void operator()() { /* do stuff for this user input */ }
};
class SecondCommand : public Command {
// some private data + constructor
public:
virtual void operator()() { /* do stuff for this user input */ }
};
A parser would create either FirstCommand or SecondCommand, and store them in the std::queue<Command*> or something more sophisticated. Your consumer code would then execute every command by doing something like:
while (!q.empty() {
Command* command = q.front();
(*command)();
q.pop();
}
With thread-safe queues, the consumer code can even be run in parallel to your parser.
You could use a queue of simple pointers to functions instead of command objects, but if you do, their signatures will have to be the same, while a constructor for a specific command can be arbitrary.
You could create a map of functions. Like this:
typedef void (*ftype)(void);
map<string, ftype> function_map;
Now you map all your functions to a command:
function_map["A"] = functionA;
function_map["B"] = functionB;
function_map["C"] = functionC;
Now after you take the user input and parse it, you can just do:
//foreach command
function_map[command]();
Here's a short code demo with primitive parsing
Could be an alternative, not exact answer. Write a computer program that parses input and sends the commands in binary through the serial. This way serial read (which is quite slow even at 115200) and parsing would be averted. As a bonus, your application has the possibility of being more user-friendly compared to a terminal.
Is there any command in gdb by which one can apply breakpoint at the start of each function in a .C file.
I need this as i have a very big C file which i need to debug and it contains more than 100 functions and i need to find all the functions called during run time.
rbreak file:regex
If you look at this page : http://sourceware.org/gdb/download/onlinedocs/gdb/Set-Breaks.html#Set-Breaks
Look also at the past thread : gdb, set breakpoint on all functions in a file
I'm not sure if it's a good idea to use the debugger that way for solving your search.
I would add at the beginning of each function a single assignment and one struct at the file start.
struct {
int foo;
int bar;
...
int lastFunctionName;
} sFunc;
void foo()
{
sFunc.foo=1;
...
}
void bar()
{
sFunc.bar=1;
...
}
Then you can run your program and it collects the informations for you.
The manual states that there is an option rbreak regexp which lets you set a regexp to break on all functions matching that regexp. Given that you are in one file (one module?) maybe all the functions are prefixed in the same way?
I want to start a simple program when windows start but I don't want to show the console output window associated with that program. Just to test, the program can be as simple as:
int main (int argc, char** argv)
{
while (1)
{
printf ("hello world...\n");
Sleep (10000);
}
return 0;
}
compiling it as: cl foo.c
I know how to start the program when windows is starting (putting in startup folder, creating registry etc), but I don't know how to hide the console output window that comes when the program is started.
While searching for this, I found that I can use start /B foo.exe. Is there any other method for doing this? some thing like "&" we use on unix.
My actual program is big and is written in C, so I can not use some other alternatives i found for c# (like WinMain) and java (like explained here).
This should work.
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdParam, int iCmdShow)
{
for (;;) {
//action here
}
return 0;
}
WinMain is not a C# entry point. C# uses a method of a static class; it is called Main by default, but can be configured to any static-class method.
In Windows, non-console C programs should define a WinMain. You can then link them using the linker's /subsystem:windows command-line option, invokable either directly or from CL.
One method is calling FreeConsole() as first thing in main. It will hide the console window, if any.
Unfortunately simply setting FreeConsole() as the first function in main allows the window to appear momentarily anyway. And FreeConsole removes the window so that if you wish to use it later( as in killing the process ) you have to make it appear on screen in a mode out of your control.
Windows allows Win32 programs to have only one of four contexts under Visual Studio: Window program with initial GUI window, Console program with initial Console window, DLL or Lib. Changing the subsystem to a non-Console choice from the project->Properties->System view only results in linking issues that block the build.
Here is what worked for me with only a little effort. Use Mike's approach above and choose Win32 Project with Window Application. Then delete everything in WinMain after the "Place code here" direction and delete all the called functions. Return true or 1, as you wish from WinMain. No window of any type will appear on launch.
And when you are ready to deal with a Console Window call AllocConsole() in your code and deal with its positioning and size as you see fit. The Console can be positioned off screen and slid into view if you wish; it only takes a few minutes to get the handle on the configuring functions. Start with Microsoft's 'Console Functions' in MSDN documents. Unfortunately, there is no book on how to use all the functions properly as there is for NCurses in Linux.
When you're creating your project create one with WinMain instead ( Win32 Project ). If you still want the console later use AllocConsole() and FreeConsole().
If the program would be eg procexp.exe you can do this out of the box :
cmd /c "start C:\Users\denni\OneDrive\_bin\_tools\ProcessExplorer\procexp.exe"
WinMain isn't unique to C#. It's possible to write GUI applications in C too. The WinMain function is the entry point, but nothing says you have to actually create a window. You could have WinMain do nothing more than call the first function of your program to get it started. Then you'd have your program running with no GUI window and no console window.
Of course, this also means no easy way to stop it, short of killing it from Task Manager.
I am trying to pass a string (or char*) from Rundll32 to a DLL built (with MinGW) using this source:
#include <windows.h>
__declspec( dllexport ) int hello(LPSTR content) {
MessageBox( NULL, content, "Message", MB_OK );
return 0;
}
When running this I get random crashes. This is how I run it.
C:\workspace>c:\MinGW\bin\gdb.exe rundll32 -ex "run program1.dll,hello test"
I tried setting a breakpoint at hello() and it seems that "content" is pretty random. Am I passing the argument from rundll32 in the wrong way?
It works fine if I don't have arguments.
rundll32 entry points need to be declared in a very specific way. Check out this article which explains what to do. One thing I've noticed is that, for a symbol called "EntryPoint", the function name passed to rundll32 should be "_EntryPoint#16" for 32-bit DLLs, and just "EntryPoint" for 64-bit DLLs.