Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Visual C++/MFC/WTL > Создание процесса.


Автор: H0Jlb 15.5.2010, 14:01
Вот сама программа. Ошибок нет, но и нечего не происходит.

Код

#pragma endregion
    private: HANDLE handle;
    private:char* StringToChar(String^ str1)
    {
        char *ch;
        pin_ptr<const wchar_t> wch = PtrToStringChars(str1);
        size_t convertedChars = 0; 
        size_t  sizeInBytes = ((str1->Length + 1) * 2); 
        ch = (char *)malloc(sizeInBytes); 
        wcstombs_s(&convertedChars, ch, sizeInBytes, wch, sizeInBytes);
        return ch;
    }
    private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb=sizeof(STARTUPINFO);
    String^  str = textBox1->Text;
    //TCHAR szCom [] =(LPWSTR) StringToChar (str);
    
    
    CreateProcess(NULL,(LPWSTR) StringToChar (str),NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi);
    handle = pi.hProcess;
             }
    
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
            TerminateProcess(handle,NULL);

             }
    };
}


Добавлено через 12 минут и 54 секунды
Вот так работает.
Код

#pragma endregion
    private: HANDLE handle;
    private:char* StringToChar(String^ str1)
    {
        char *ch;
        pin_ptr<const wchar_t> wch = PtrToStringChars(str1);
        size_t convertedChars = 0; 
        size_t  sizeInBytes = ((str1->Length + 1) * 2); 
        ch = (char *)malloc(sizeInBytes); 
        wcstombs_s(&convertedChars, ch, sizeInBytes, wch, sizeInBytes);
        return ch;
    }
    private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb=sizeof(STARTUPINFO);
    //String^  str = textBox1->Text;
    //TCHAR szCom [] =(LPWSTR) StringToChar (str);
    [U]TCHAR szCom []= TEXT ("NOTEPAD");
    
    CreateProcess(NULL,szCom,NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi);
[/U]
    handle = pi.hProcess;
             }
    
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
            TerminateProcess(handle,NULL);

             }
    };
}



Но я хочу сам указывать процесс.

Автор: GoldFinch 15.5.2010, 14:26
H0Jlb, используй CreateProcessA
StringToChar возвращает char*
а у тебя CreateProcessW, ему надо LPWSTR, т.е. whar_t

Автор: H0Jlb 15.5.2010, 14:57
Я прочитал про CreateProcessA, примерно понял чем отличается. Но как исправить тоесть ну впишу я а остальное так же оставить?

Автор: GoldFinch 15.5.2010, 15:16
Код

CreateProcessA(NULL, StringToChar(str),NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi);

Автор: H0Jlb 15.5.2010, 16:19
Ошибка    1    error C2664: CreateProcessA: невозможно преобразовать параметр 9 из 'STARTUPINFO *' в 'LPSTARTUPINFOA'    c:\users\gss-belui_mag\documents\visual studio 2008\projects\lb3\lb3\Form1.h    137    lb3

Автор: GoldFinch 15.5.2010, 16:43
H0Jlb, 
либо используй StringToChar и ANSI варианты структур и функций (STARTUPINFOA),
либо отключи юникод в настройках проекта,
либо используй не StringToChar, а что-то другое, то что возвращает wchar_t*

Автор: H0Jlb 16.5.2010, 09:06
 smile   smile  smile 

Автор: xTr1m 18.5.2010, 17:34
Может поможет (это вместо stringtochar)
Код

LPWSTR GetWCHAR(CString str)
{
    LPWSTR lpszW = new WCHAR[str.GetLength()+1]; //where sText is the CString variable.
    LPTSTR lpstr = str.GetBuffer(str.GetLength());
    int nLen = MultiByteToWideChar(CP_ACP,0,lpstr,-1,NULL,NULL);
    MultiByteToWideChar(CP_ACP,0,lpstr,-1,lpszW,nLen);

    return lpszW;
} 

не забываем потом ручками удалять.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)