| View previous topic :: View next topic |
| Author |
Message |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Sun Aug 24, 2008 9:39 pm Post subject: Using command line in C++. |
|
|
| I'm well aware of using system(), but I'm pretty sure there is a better way of doing what I'm trying to do. Basically, I'm trying to emulate the ipconfig/release and ipconfig/renew commands in command prompt from my program. Is there any efficient way to do this?
|
|
| Back to top |
|
 |
sponge I'm a spammer
Reputation: 1
Joined: 07 Nov 2006 Posts: 6009
|
Posted: Sun Aug 24, 2008 10:50 pm Post subject: |
|
|
ipconfig is a command line alias. define what you're trying to emulate. the way ipconfig is invoked or the actual ip shit.
_________________
|
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Sun Aug 24, 2008 11:04 pm Post subject: |
|
|
| Code: |
ShellExecute(
NULL,
_T("open"),
_T("cmd"),
_T(" /C ipconfig"), // params
_T(" C:\ "),
SW_SHOW);
|
idk could be helpful
/C means run command
so like cmd.exe /c ipconfig works.. so uhhh yah.. but i see what u want and i do think its possible cuz I've seen it done before just dont remember
_________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Mon Aug 25, 2008 4:11 am Post subject: |
|
|
http://msdn.microsoft.com/en-us/library/aa394217.aspx :
# ReleaseDHCPLease Method of the Win32_NetworkAdapterConfiguration Class
# ReleaseDHCPLeaseAll Method of the Win32_NetworkAdapterConfiguration Class
# RenewDHCPLease Method of the Win32_NetworkAdapterConfiguration Class
# RenewDHCPLeaseAll Method of the Win32_NetworkAdapterConfiguration Class
WMI.. You'll be having fun with this one :P Well, as suggested, creating an external process (ipconfig) will be a bit more easier to implement.
PS. I barely know anything about WMI :)
|
|
| Back to top |
|
 |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Mon Aug 25, 2008 9:21 am Post subject: |
|
|
Thanks for all your help. I tried xor's method, but I came across a typecast error. Basically from LPVOID to LPWSTR. It has to do with allowing lpSystemPath accessing the memory allocated by the heap. I'm not sure about how to go and correct this so help would be appreciated.
Here's my source right now:
| Code: |
#include "windows.h"
#include "resource.h"
#include "stdio.h"
#include "tchar.h"
HWND hWnd;
LPWSTR Release[] = { L"/release"};
LPWSTR Renew[] = { L"/renew"};
BOOL ExecuteIPConfigCommands(__in_ecount_z(nSize) LPWSTR lpArguments[], __in UINT nSize);
LRESULT CALLBACK DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, (DLGPROC)DialogProc);
RegisterHotKey(hWnd, 12, 0, 112);//F1
RegisterHotKey(hWnd, 13, 0, 113);//F2
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
return TRUE;
case WM_HOTKEY:
switch(wParam)
{
case 12:
ExecuteIPConfigCommands(Release, 1);
return TRUE;
case 13:
ExecuteIPConfigCommands(Renew, 1);
return TRUE;
}
}
return FALSE;
}
BOOL ExecuteIPConfigCommands(__in_ecount_z(nSize) LPWSTR lpArguments[], __in UINT nSize)
{
LPTSTR lpSystemDirectory = NULL;
HANDLE hHeap;
BOOL bRET = FALSE;
UINT i;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
hHeap = GetProcessHeap();
if (hHeap != NULL)
{
lpSystemDirectory = HeapAlloc(hHeap, 0, MAX_PATH*sizeof(TCHAR));
if (lpSystemDirectory != NULL)
{
if (GetSystemDirectory(lpSystemDirectory, MAX_PATH) != 0)
{
for (i = 0; i < nSize; i++)
{
bRET = CreateProcess(TEXT("ipconfig.exe"), lpArguments[i], NULL, NULL, FALSE, 0, NULL, lpSystemDirectory, &si, &pi);
}
}
}
}
return 0;
}
| I know I'm doing stupid stuff with allocating global variables and crap. I'm gonna fix that stuff later, I just want to get this working first before worrying about efficiency.
EDIT: I got rid of the heap stuff and just defined lpSystemDirectory to be TCHAR[], it compiled, and didn't do anything. I'm not sure what's going on now.
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Aug 25, 2008 1:08 pm Post subject: |
|
|
Ipconfig.exe has really pretty internals, check it out in IDA some time.
You could load ipconfig.exe with LoadLibrary and figure you the arguments to DoRelease and DoRenew, but you can't you GetProcAdress to find those since they are not exported.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Aug 25, 2008 5:33 pm Post subject: |
|
|
Shouldn't it be:
or
_________________
|
|
| Back to top |
|
 |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Mon Aug 25, 2008 5:58 pm Post subject: |
|
|
| Meh, I used the code x0r/lurc both provided, and it doesn't seem to work at all. It compiles, but nothing happens when I pass L"/release" and L"/renew" as arguments.
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Aug 25, 2008 6:07 pm Post subject: |
|
|
I would reccomend Jani's approach.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Mon Aug 25, 2008 6:18 pm Post subject: |
|
|
| Those require reboot.
|
|
| Back to top |
|
 |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Mon Aug 25, 2008 6:30 pm Post subject: |
|
|
Yeah I know its an array. This is the relevant code:
| Code: |
...
case WM_HOTKEY:
switch(wParam)
{
case 12:
ExecuteIPConfigCommands(Release, 1);
return TRUE;
case 13:
ExecuteIPConfigCommands(Renew, 1);
...
|
And
| Code: | LPWSTR Release[] = { L"release"};
LPWSTR Renew[] = { L"renew"}; |
EDIT:Ah I think it has something to do with the way I'm passing the arguments to the command line. I think its sending each letter as opposed to the whole argument. You guys know how to correct this?
|
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Mon Aug 25, 2008 6:58 pm Post subject: |
|
|
run the array backwards in a loop with a swap function
NVM
| Code: |
const int nSize = 5;
int anArray[nSize] = { 50, 40, 30, 20, 10 };
for (int nStartIndex = 0; nStartIndex < nSize; nStartIndex++)
{
int t = anArray[nStartIndex];
anArray[nStartIndex] = anArray[nStartIndex+1];
anArray[nStartIndex+1] = t;
//o its same as..
//swap(anArray[nStartIndex],anArray[nStartIndex+1]);
}
|
thats more like it
found something odd on google!
u could also do this but dont know if it works
| Code: |
for (int nStartIndex = 0; nStartIndex < nSize; nStartIndex++)
{
anArray[nStartIndex] ^= anArray[nStartIndex+1];
anArray[nStartIndex+1] ^= anArray[nStartIndex];
anArray[nStartIndex] ^= anArray[nStartIndex+1];
}
|
can someone explain why this one works if it does? i wanna know myself with xor's isn't it doing a bit too much
nvm it might of been a joke? yah i dont want to go to the hassle of testing it.. but it shouldnt of worked cuz if it xors values.. should l give bad values on the otherside i've tested the ^ operator on google and it doesnt look good. so w\e
_________________
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Tue Aug 26, 2008 2:14 am Post subject: |
|
|
dhcpsvc.dll
DhcpReleaseParameters
DhcpAcquireParameters
That is what ipconfig.exe uses.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
|