Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C# - Dll questions

28 views
Skip to first unread message

Mr. X.

unread,
May 23, 2012, 9:41:48 AM5/23/12
to
Hello,

In C# ...
1. I have tried to write to Console : "Hello world" by
Console.WriteLine("Hello world"), but I cannot see the console screen.
2. How can I show a MessageBox in a class Library (dll) instead of console.
Need some samples, please.

Thanks :)

Peter Duniho

unread,
May 23, 2012, 10:22:05 AM5/23/12
to
On Wed, 23 May 2012 16:41:48 +0300, Mr. X. wrote:

> Hello,
>
> In C# ...
> 1. I have tried to write to Console : "Hello world" by
> Console.WriteLine("Hello world"), but I cannot see the console screen.

The usual way to enable that is to set your project type to a console
application, so that Windows knows to create and attach a console window to
your process when it starts. See your project properties for details, or
simply choose the console application type when creating the project in the
first place.

> 2. How can I show a MessageBox in a class Library (dll) instead of console.
> Need some samples, please.

MessageBox works the same in whatever kind of project type you use it.
It's not all that common to use MessageBox in a console application, but
the usage is the same whether in a console application or a DLL or
something else.

Pete

bradbury9

unread,
May 23, 2012, 12:00:02 PM5/23/12
to
To call MessageBox.Show function in a class library you must add a reference to System.Windows.Forms

It would be wise to provide a sample code of the non working Console.WriteLine, so we can look at it and point out the error.

Mr. X.

unread,
May 24, 2012, 7:21:06 AM5/24/12
to
Well, that's what I have done.
Still on the class library - MessageBox.Show("x"); isn't compiled.
I got the message:
Error 1 The name 'MessageBox' does not exist in the current context

I use C# VS 2008.

Thanks :)

"bradbury9" wrote in message
news:c883c5ac-0d0e-4343...@googlegroups.com...

El mi�rcoles, 23 de mayo de 2012 15:41:48 UTC+2, Mr. X. escribi�:

Mr. X.

unread,
May 24, 2012, 7:37:26 AM5/24/12
to
First - it just for debug ...
(It's dll : Class library).
I need somehow to trace the dll (also by messages).

Thanks :)

"Mr. X." wrote in message news:jpl5j3$r1e$1...@dont-email.me...

Well, that's what I have done.
Still on the class library - MessageBox.Show("x"); isn't compiled.
I got the message:
Error 1 The name 'MessageBox' does not exist in the current context

I use C# VS 2008.

Thanks :)

"bradbury9" wrote in message
news:c883c5ac-0d0e-4343...@googlegroups.com...

Peter Duniho

unread,
May 24, 2012, 10:21:08 AM5/24/12
to
On Thu, 24 May 2012 14:21:06 +0300, Mr. X. wrote:

> Well, that's what I have done.
> Still on the class library - MessageBox.Show("x"); isn't compiled.
> I got the message:
> Error 1 The name 'MessageBox' does not exist in the current context

To use any member of any type, you must:

-- reference the assembly that declares the type, and
-- use the correct name in your code when using the type

If a type exists within a namespace, as most do (including MessageBox), you
must either provide the fully-qualified name (i.e. the full name, including
the namespace) or you must add an appropriate "using" directive to your .cs
file for the namespace in which the type is found).

Given the error message, I suspect you have not added the necessary "using"
directive. You either need to do that, or use the fully-qualified name of
the type (which is "System.Windows.Forms.MessageBox").

Pete

Mr. X.

unread,
May 24, 2012, 10:23:33 AM5/24/12
to
The application is class library,
so I put on code :
[DllImport("user32.dll", EntryPoint = "MessageBoxA")]
static extern int MsgBox(int hWnd,
string msg,
string caption,
int type);

and use MsgBox fine.

Thanks, anyway :)


"Peter Duniho" wrote in message
news:1l9844fe2bn0x$.y0xgjw98flkv$.dlg@40tude.net...

Peter Duniho

unread,
May 24, 2012, 10:30:49 AM5/24/12
to
On Thu, 24 May 2012 17:23:33 +0300, Mr. X. wrote:

> The application is class library,
> so I put on code :
> [DllImport("user32.dll", EntryPoint = "MessageBoxA")]
> static extern int MsgBox(int hWnd,
> string msg,
> string caption,
> int type);
>
> and use MsgBox fine.

That's a stupid way to display a message box from managed code.

> Thanks, anyway :)

You're welcome.

Mr. X.

unread,
May 24, 2012, 10:33:42 AM5/24/12
to
System.Windows.Form is not acceptable (it isn't compiled - The computer
doesn't know this - Framework 3.5).

"Peter Duniho" wrote in message
news:t2qzk50dkjr8$.aowg5titiy2i.dlg@40tude.net...

Peter Duniho

unread,
May 24, 2012, 10:51:34 AM5/24/12
to
On Thu, 24 May 2012 17:33:42 +0300, Mr. X. wrote:

> System.Windows.Form is not acceptable (it isn't compiled - The computer
> doesn't know this - Framework 3.5).

Since "System.Windows.Form" is not what any of us wrote, I'm not all that
worried about it.

You should become familiar with http://msdn.microsoft.com/ and especially
http://msdn.microsoft.com/library

There is a wealth of useful information and documentation there, including
detailed reference of the C# language and the .NET Framework libraries.

Pete

Mr. X.

unread,
May 24, 2012, 11:30:02 AM5/24/12
to
For http://social.msdn.microsoft.com/Search/en-US?query=messagebox&ac=8
(In the same site, and many samples around the internet -
http://social.msdn.microsoft.com/Search/en-US?query=messagebox&ac=8
the namespace is System.Windows.Forms (the only "s" for different) - Not
working neighther).

"Peter Duniho" wrote in message
news:u3kbwsjacezm.10...@40tude.net...

bradbury9

unread,
May 24, 2012, 12:10:54 PM5/24/12
to
El jueves, 24 de mayo de 2012 16:23:33 UTC+2, Mr. X. escribió:
> The application is class library,
> so I put on code :
> [DllImport("user32.dll", EntryPoint = "MessageBoxA")]
> static extern int MsgBox(int hWnd,
> string msg,
> string caption,
> int type);
>
> and use MsgBox fine.
>
> Thanks, anyway :)

- Do no reinvent the wheel, use .NET API so you have less code to debug.

- You can call .NET API MessageBox.Show since easily http://msdn.microsoft.com/es-es/library/system.windows.forms.messagebox.show%28v=vs.80%29.aspx

- If you port your code to other OS (other versions o windows, smart devices, linux) you won't need to change your code to make it work again.

- yeah, it is not compiled but... Aren't the pros better than the cons?


Peter Duniho

unread,
May 24, 2012, 3:08:38 PM5/24/12
to
On Thu, 24 May 2012 18:30:02 +0300, Mr. X. wrote:

> For http://social.msdn.microsoft.com/Search/en-US?query=messagebox&ac=8
> (In the same site, and many samples around the internet -
> http://social.msdn.microsoft.com/Search/en-US?query=messagebox&ac=8
> the namespace is System.Windows.Forms (the only "s" for different) - Not
> working neighther).

"Not working neighther" is not a useful problem description (never mind
good grammar).

If you want help, you need to get into the habit of asking good questions.

Mr. X.

unread,
May 28, 2012, 1:27:52 AM5/28/12
to
Just needed to add reference to System.Windows.Forms.

Still, I don't know why the command console doesn't return an output to
basic console window (or how can I show the console window - The output type
is class library).

Thanks, anyway :)

"Peter Duniho" wrote in message
news:dfpgmr47fd5i.1e957ova14san$.dlg@40tude.net...
0 new messages