且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

控制台窗口问题

更新时间:2023-02-19 19:38:34

我改变了一下代码:


AllocConsole();

IntPtr con = GetStdHandle( -11);

SetConsoleTitle(" xexexe");

IntPtr intptr = new IntPtr(0);

uint dwNumberOfCharsWritten = 0;

string wbuffer =" anthonyb";

WriteConsole



con,

wbuffer,

(uint)wbuffer.Length,

ref dwNumberOfCharsWritten,

intptr // reserved

);

抛出新的Win32Exception(Marshal.GetLastWin32Error());


a现在,我可以看到错误是什么:指定的模块不能

找到了

如果这意味着控制台窗口句柄,我可以看到con值总是

有相同的价值...


这方面的任何帮助???


非常感谢任何帮助


p /调用声明:

---------------------

[DllImport(" kernel32",SetLastError = true,ExactSpelling = true)]

static extern bool AllocConsole();

[DllImport(" ; kernel32.dll")]

static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport(" kernel32.dll")]

static extern bool SetConsoleTitle(string lpConsoleTitle);

[DllImport(" Kernel32.DLL",EntryPoint =" WriteConsoleW",

CallingConvention = CallingConvention.StdCall)]

public static extern bool WriteConsole



IntPtr hConsoleOutput,//句柄屏幕缓冲区

string lpBuffer,//写入缓冲区

uint nNumberOfCharsToWrite,//要写的字符数

ref uint lpNumberOfCharsWritten,//写入的字符数

IntPtr lpReserved //保留

);
i changed a bit the code to this:

AllocConsole();
IntPtr con = GetStdHandle(-11);
SetConsoleTitle("xexexe");
IntPtr intptr = new IntPtr(0);
uint dwNumberOfCharsWritten = 0;
string wbuffer = "anthonyb";
WriteConsole
(
con,
wbuffer,
(uint) wbuffer.Length,
ref dwNumberOfCharsWritten,
intptr // reserved
);
throw new Win32Exception(Marshal.GetLastWin32Error());

and now i can see what the error is: "The specified module could not be
found"
If it means the console window handle, i can see that the con value always
has same value...

Any help on this ???

thanks a lot for any help

p/invoke declarations:
---------------------
[DllImport("kernel32", SetLastError=true, ExactSpelling=true)]
static extern bool AllocConsole();
[DllImport("kernel32.dll")]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool SetConsoleTitle(string lpConsoleTitle);
[DllImport("Kernel32.DLL", EntryPoint="WriteConsoleW",
CallingConvention=CallingConvention.StdCall)]
public static extern bool WriteConsole
(
IntPtr hConsoleOutput, // handle to screen buffer
string lpBuffer, // write buffer
uint nNumberOfCharsToWrite, // number of characters to write
ref uint lpNumberOfCharsWritten, // number of characters written
IntPtr lpReserved // reserved
);


objectref,


一旦你调用了AllocConsole,你就可以在System命名空间的Console类中调用静态方法,并写下你想要的
。没有必要通过P / Invoke层将

写入控制台。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam.guard.caspershouse.com


" objectref" < OB ******* @ mediatrel.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
objectref,

Once you make the call to AllocConsole, you should be able to just call
the static methods on the Console class in the System namespace, and write
what you want. There is no need to go through the P/Invoke layer to write
to the console.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
您好所有,

我有一个窗口应用程序,我想在控制台窗口中显示一些信息。
我想出来(经过很长时间的搜索......)我该怎么做它

我尝试使用以下代码。

问题是ok设置为true,con设置为一个数字(一切都很好
直到这里)
控制台窗口出现但字符串anthonyb永远不会显示
那里......

任何人都可以帮助我,并指出我做错了什么?

bool ok = AllocConsole();
IntPtr con = GetStdHandle(-11);
uint写的;
WriteConsole(con,anthonyb,8,写出来,IntPtr.Zero);

非常感谢您的帮助,

objectref
Hi to all,

i have a window app and i want to display some info in a console window.
I figured out (after a very long search...) how am i supposed to do it
and
i try using the following code.

The problem is that ok is set to true, con is set to a number (all fine
until here)
the console window appears but the string "anthonyb" is never displayed
there...

Can anyone help me and point out to me what am i doing wrong ??
bool ok = AllocConsole();
IntPtr con = GetStdHandle(-11);
uint written;
WriteConsole(con, "anthonyb", 8, out written, IntPtr.Zero);
Thanks a lot for any help,

objectref



您好Nicholaw并感谢您的帮助。

你确定这样工作吗?

我的意思是,这是一个窗口应用程序,我打开一个控制台窗口和所有的搜索

i在过去2小时,给了我一些我发布的代码,

带p / invoke和WriteConsole的东西...


objectref

Nicholas Paldino [.NET / C#MVP]" &LT; mv*@spam.guard.caspershouse.com>写在

消息新闻:Og ************** @ TK2MSFTNGP11.phx.gbl ...
Hi Nicholaw and thanks for your help.
Are you sure that is working this way ?
I mean, this is a window app that i open a console window and all the search
i did in the last 2 hours, gave to me sort-of the code that i posted,
with p/invoke and WriteConsole stuff...

objectref
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Og**************@TK2MSFTNGP11.phx.gbl...
objectref,>
一旦你调用了AllocConsole,你应该能够在System命名空间中调用Console类的静态方法,并写下你想要的东西。没有必要通过P / Invoke层来写入控制台。

希望这会有所帮助。

-
- 尼古拉斯Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

objectref &LT; OB ******* @ mediatrel.com&GT;在消息中写道
新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
objectref,

Once you make the call to AllocConsole, you should be able to just call
the static methods on the Console class in the System namespace, and write
what you want. There is no need to go through the P/Invoke layer to write
to the console.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
大家好,

我有一个窗口应用程序,我想在控制台窗口中显示一些信息。
我想出来(经过很长时间的搜索...)我应该怎么做

我尝试使用以下代码。

问题是ok设置为true,con设置为一个数字(一切都很好
直到这里)
出现控制台窗口但字符串anthonyb永远不会显示
那里......

任何人都可以帮助我,并指出我做错了什么?

bool ok = AllocConsole();
IntPtr con = GetStdHandle(-11);
uint写的;
WriteConsole(con,anthonyb,8,写出来,IntPtr.Zero);

非常感谢您的帮助,

objectref
Hi to all,

i have a window app and i want to display some info in a console window.
I figured out (after a very long search...) how am i supposed to do it
and
i try using the following code.

The problem is that ok is set to true, con is set to a number (all fine
until here)
the console window appears but the string "anthonyb" is never displayed
there...

Can anyone help me and point out to me what am i doing wrong ??
bool ok = AllocConsole();
IntPtr con = GetStdHandle(-11);
uint written;
WriteConsole(con, "anthonyb", 8, out written, IntPtr.Zero);
Thanks a lot for any help,

objectref