|
|
Hi David,
I am not looking to write something by WriteConsole()and not looking to redirect one screen out put to the other
i am looking to execute a command once the cmd.exe process has started. how can i do this?
Thanks,
Dev.
|
|
|
|
|
Hi Dev.
I suggest that you research and seek to understand STDIN STDOUT STDERR[^] because when you open a command prompt and you type DIR you are actually writing to the STDIN stream. After you research these streams you will have a better understanding of my answer.
devgo wrote: i am looking to execute a command once the cmd.exe process has started. how can i do this?
You have been given several options.
Option 1.) Pass a command on the CreateProcess command line.
>cmd /?
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
Option 2.) Interact with the standard input handle and pass commands.
How to spawn console processes with redirected standard handles[^]
These are your best options if you are using CreateProcess to open the command prompt as you described in your original post.
Here is another Microsoft sample of redirecting the input/output handles.
Creating a Child Process with Redirected Input and Output[^]
Best Wishes,
-David Delaune
|
|
|
|
|
Hi David,
Thank you very much for your help. i got the work done with option 1). i am a new bee for windows. i will look for the option 2) as well.
Thanks for your inputs.
--Dev
|
|
|
|
|
Hi David,
As you were aware, i am trying to execute a command in the console window created through createprocess(); now i am trying to execute the command by using Pipes as you suggested.
I am pasting the code here. i am not able to print the string on the console window created. could you please suggest what i am doing wrong.
/********************************CODE ******************************* /
void main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD i;
HANDLE hout;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
HWND hWnd = NULL;
TCHAR buf[128];
HANDLE g_hChildStd_IN_Rd = NULL;
HANDLE g_hChildStd_IN_Wr = NULL;
HANDLE g_hChildStd_OUT_Rd = NULL;
HANDLE g_hChildStd_OUT_Wr = NULL;
SECURITY_ATTRIBUTES saAttr;
DWORD dwRead, dwWritten;
CHAR chBuf[128];
BOOL bSuccess = FALSE;
// Set the bInheritHandle flag so pipe handles are inherited.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// Create a pipe for the child process's STDOUT.
if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) )
printf("StdoutRd CreatePipe");
// Ensure the read handle to the pipe for STDOUT is not inherited.
if ( ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) )
printf("Stdout SetHandleInformation");
// Create a pipe for the child process's STDIN.
si.hStdError = g_hChildStd_OUT_Wr;
si.hStdOutput = g_hChildStd_OUT_Wr;
si.hStdInput = g_hChildStd_OUT_Rd;
//si.dwFlags |= STARTF_USESTDHANDLES;
// Start the child process.
if( !CreateProcess("C:\\windows\\system32\\cmd.exe", // No module name (use command line).
NULL,//// Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
CREATE_NEW_CONSOLE, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
bSuccess = WriteConsole(g_hChildStd_OUT_Rd, "dir", 3, &dwWritten, NULL);
CloseHandle(g_hChildStd_OUT_Rd);
/***********************************************************************************/
|
|
|
|
|
 Hi Devgo,
In the future you should not reply to yourself. You are lucky that I review my old threads... I did not get notification of your response because you replied to yourself. Anyway I wrote a quick and dirty sample for you. In this example I am creating an instance of CMD.EXE and writind the Dir command to its STDIN pipe. I then use MessageBox to show the results.
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <process.h>
#pragma comment(lib,"user32.lib")
HANDLE g_hChildStd_IN_Rd = NULL;
HANDLE g_hChildStd_IN_Wr = NULL;
HANDLE g_hChildStd_OUT_Rd = NULL;
HANDLE g_hChildStd_OUT_Wr = NULL;
unsigned __stdcall ReadCMDThread(void* pArguments)
{
DWORD dwAvailable =0;
DWORD bytesRead =0;
CHAR szOut[4096] = {0};
while(0 == bytesRead)
{
PeekNamedPipe(g_hChildStd_OUT_Rd,szOut,sizeof(szOut),&bytesRead,&dwAvailable,NULL);
if(0 != bytesRead)
{
Sleep(1000);
ReadFile(g_hChildStd_OUT_Rd,szOut,sizeof(szOut),&bytesRead,NULL);
::MessageBoxA(NULL,szOut,"",MB_OK);
break;
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD bytesRead;
DWORD exitcode;
PROCESS_INFORMATION pi;
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
ZeroMemory(&sa,sizeof(sa));
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle=TRUE;
CreatePipe(&g_hChildStd_IN_Rd,&g_hChildStd_IN_Wr,&sa,0);
CreatePipe(&g_hChildStd_OUT_Rd,&g_hChildStd_OUT_Wr,&sa,0);
ZeroMemory(&si,sizeof(si));
GetStartupInfo(&si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.hStdOutput = g_hChildStd_OUT_Wr;
si.hStdError = g_hChildStd_OUT_Wr;
si.hStdInput = g_hChildStd_IN_Rd;
si.wShowWindow = SW_HIDE;
BOOL bSuccess = CreateProcess(
_T("C:\\windows\\system32\\cmd.exe"),
NULL,
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);
UINT threadID;
HANDLE hThread = (HANDLE)_beginthreadex(NULL,0,&ReadCMDThread,NULL,0,&threadID);
Sleep(500);
if(INVALID_HANDLE_VALUE != hThread)
{
DWORD dwWritten = 0;
CHAR szIN[4096];
strcpy(szIN,"dir\n");
WriteFile(g_hChildStd_IN_Wr,szIN,strlen(szIN),&dwWritten,NULL);
WaitForSingleObject(hThread,INFINITE);
CloseHandle(hThread);
strcpy(szIN,"exit\n");
WriteFile(g_hChildStd_IN_Wr,szIN,strlen(szIN),&dwWritten,NULL);
}
CloseHandle(g_hChildStd_IN_Rd);
CloseHandle(g_hChildStd_IN_Wr);
CloseHandle(g_hChildStd_OUT_Rd);
CloseHandle(g_hChildStd_OUT_Wr);
return 0;
}
The code is ugly and contains some Sleeps to allow CMD.EXE to initialize. I also use Sleep to wait for the Read pipe to become full. There are probably better ways to do this. Also... on my Windows7 workstation I had problems terminating CMD.exe by closing its handles. Using TerminateProcess is generally not desirable so I opted to write exit to the STDIN stream.
There are some other functions that you might want to get familiar with:
WriteConsoleInput Function[^]
GenerateConsoleCtrlEvent Function[^]
Good Luck,
-David Delaune
|
|
|
|
|
I want to use different activeX files (for instance release/debug) versions and to do so i need to register the activeX according to realtive path (installation will be in different places).
Is there a way to do so, so that both version work?
Thanks,
Eyal
|
|
|
|
|
when you register an ActiveX object, its CLSID is added to HKEY_CLASSES_ROOT/CLSID and a path to the DLL is added below that. so if the debug and release versions use the same CLSIDs for their interfaces (which they typically do), there's no way to have multiple versions, regardless of where the DLLs live.
|
|
|
|
|
Greetings!
We have just started working in VS 2008. Our code, written in C++, often stores SQL select statements in string variables. These strings are often longer than the debugger can display. Is there a way to copy the entire string onto the clipboard so I can paste it into Notepad or an SQL command window or some such thing?
Thank you very much!
RobR
|
|
|
|
|
Use your right mouse button to click on the value, then select "copy value"
|
|
|
|
|
there's usually a little magnifying glass next to the short string display, in the debugger watch windows. clicking that will bring up a window (the "visualizer") with the full text.
|
|
|
|
|
Hi. Well.. I need to add some tooltips to my main window (CDialog).
So... I do this in OnInit
bool test21=TTC.Create(this);
theApp.tooltip=&(TTC);
m_but.MoveWindow(0,0,500,500);
bool test11=theApp.tooltip->AddTool(m_but,"Cool?");
TTC.Activate(TRUE);
and
BOOL MainDial::PreTranslateMessage(MSG* pMsg)
{
if (NULL != theApp.tooltip)
theApp.tooltip->RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
Appears a great button, but hint doesn't. Why? Any idea? Smthng that i'am missing? Huh?
|
|
|
|
|
Could it be that in your OnInitDialog the TTC variable, which is i assume a CToolTipCtrl a local variable? If so then when your OnInitDialog is done and returns, TTC will be deleted and its destructor also destroys the tooltip. Of course that you don't get a crash in PreTranslateMessage suggests this is not the case but it might happen. Try making your TTC a member of the dialog class if it is not and use that instance instead of storing a pointer at it.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
Well.. My CToolTipCtrl is member of MainDial of course. And by the way i already made it work with this button, but now i have another trouble. I create lots of dynamic buttons, inside of MPictureBox.Create i write:
BOOL result = CButton::Create(lpszCaption,dwStyle,rect,pParentWnd,nID);
this->SetParent(pParentWnd);
theApp.tooltip->AddTool(this,"HM?!");
but once again it doesn't help. Hint doesn't appear. Any idea?
|
|
|
|
|
You probably need to relay messages received by your picture box also to the tooltip control not only ones targeted at your dialog box. Aside of that, no idea. Just out of curiosity, what was your first problem?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
My original problem was to make some sort of game. Jackal. It's a board game invented 30-40 years ago at MGU (principal university of Moscow). For now i made it, but it's possible to play it only for humans against humans on the same cpu (hot seat mode). (Later i plan to make net support and cpu gamers). If you want to see what i have done for now drop.io/shchepin/ release.rar (requires redistributable package for vs2008 sp1).
P.S. I intented with
BOOL MPictureBox::PreTranslateMessage(MSG* pMsg)
{
if (NULL != theApp.tooltip)
theApp.tooltip->RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
Didn't helped... =(
|
|
|
|
|
What kind of a control is that picturebox?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
Just like i said before, it's a CButton child.
|
|
|
|
|
Program for multiplication of two matrices.
/* Program for multiplication of two matrices */
# include <stdio.h>
# include <conio.h>
# define ROW 3
# define COL 3
void main()
{
int mat1[ROW][COL],mat2[ROW][COL],mat3[ROW][COL];
int i,j,k;
clrscr();
printf("\n Enter matrix mat1(%dx%d)row-wise:\n ",ROW,COL);
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
scanf("%d",&mat1[i][j]);
}
printf("\n Matrix 1 is %d \n",mat1[i][j]);
printf("\n Enter matrix mat2(%dx%d)row-wise:\n ",ROW,COL);
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
scanf("%d",&mat2[i][j]);
}
printf("\n Matrix 2 is %d \n",mat2[i][j]);
}
//Multiplication
for(i=0;i<ROW;i++)
for(j=0;j<COL;j++)
{
mat3[i][j]=0;
for(k=0;k<COL;k++)
mat3[i][j]+=mat3[i][j]+mat1[i][k] * mat2[k][j];
}
// End of multiplication
printf("\n The Resultant matrix mat3 is: \n");
for(i=0;i<ROW;i++)
for(j=0;j<COL;j++)
printf("\n %5d",mat3[i][j]);
printf("\n");
getch();
}
maine multiplication ke liye yeh code likha hai par issme koi logical error hai,jo main remove nahi kar paa raha hoo..plzzz help me..
modified on Wednesday, November 11, 2009 8:41 AM
|
|
|
|
|
nobody here is going to do your homework for you.
|
|
|
|
|
I'm not sure whether to laugh or cry.
Can you do this on paper? If not, then don't bother with trying to code it.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
42. Always. Don't bother calculating, the answer is always 42
|
|
|
|
|
You are smoking the wrong stuff. Try herbal tea.
|
|
|
|
|
Hello Forum,
I have an C++ Control (RichClient) which contains a .NET Infragistics UltraWinGrid Control. The UltraWinGrid Control isn't repainted correctly, if other controls like Topmenu / or other C++ Windows are displayed and closed on top of the UltraWinGrid Control.
Even for example embedded DateTime Controls in the UltraWinGrid Cells only repaint the parts they cover, if in the method AfterCloseUp parent.Invalidate(true) is called.
Which Strategy could help me out to ensure the repainting of the UltraWinGrid-Parts (in .NET) out of C++, which has been coverd by other C++ controls?
thanks a lot
Sebastian
|
|
|
|
|
Try responding to the underlying window's WM_ERASEBKGND message. That is, if the grid is in a dialog, respond to the dialog's WM_ERASEBKGND message, invalidating the grid there.
|
|
|
|
|