Newsgroup FAQ - please contribute

P

Pete Davis

I missed the original message, but Jon, this is an excellent start. I'm
definitely going to bookmark it. I'll start making a list of questions (and
answers) that you can browse through and add what you want. I'll post it
here, hopefully later today.

Pete
 
P

Pete Davis

Jon,

Here's what I've got. Hope it's a small help. I'll try to do more when I
have some free time.

First of all, after some thought, it occurs to me (and please take this only
as a constructive comment) that it might be best if the ASP.NET, ADO.NET and
Windows.Forms sections be removed from the FAQ. Others may disagree, but it
seems to me that each of these topics is deserving of its own faq (and some
already exist). I would think the extent that you could discuss these would
be in how they differ in C# from other languages and they don't really, as
they're all part of the framework. Just my opinion and it's no more valid
than anyone else's.

For today I only have one actual question and answer. If I had more time
(and this is a bad week, I don't have much), I would write more. I do have
some suggested questions and some notes on some of them about how they might
be answered:

Q: I have a COM object. Can I use it from C#?

A: There are several ways to use COM objects from C#. As with unmanaged
code, you can do early binding or late binding. Early binding happens at
compile time.

Early binding is faster. It also has the advantage that it's much easier to
use, particuarly in invoking methods, properties and events of a COM object.
However, it also has the limitation that you can only use a compatible
version of the COM object. If the version changes, your C# program will
break.

Late binding is done at runtime and using the ProgID of the COM object, you
can link to whatever version is installed. The disadvantage of late binding
is that it's a bit more difficult to invoke functions.

There are two ways to do early binding. The method I see most often in books
(and to me, it's the more difficult of the two), is to use TlbImp.exe to
create a .NET wrapper assembly (called a Runtime Callable Wrapper or RCW) of
the COM object. An example:

TlbImp mycomob.dll /out:mycomobRCW.dll

This will create a new DLL called mycomboRCW.dll which you can then
reference from your project.

The easier way is from within Visual Studio. Simply go to Add References and
select the COM tab. From here you can choose your COM object and the RCW
will be created automatically behind the scenes without any additional work.

Late binding is done in the following manner. Assuming that mycomob.dll has
an object called MyObjectA, you could use it as follows:

Type myObType = Type.GetTypeFromProgID("MyComOb.MyObjectA");
Object myOb = Activator.CreateInstance(myObType);

Where it gets tricky is invoking methods. This needs to be done with the
InvokeMember method. For example:

object invokeResult = myObType.InvokeMember("MyMethod",
BindingFlags.InvokeMethod,
null,
myOb,
argArray);

Whereas with early binding you could simply do:

MyComOb.MyObjectA myOb = new MyComOb.MyObjectA();
myOb.MyMethod(param1, param2, ...)

While you can manually release COM objects using Marshal.ReleaseComObject()
there is no need. As with any other object in C#, when the object goes out
of scope, the garbage collector will call the Finalizer() which in turn will
call IUnknown::Release() in the COM object.

[note to FAQ maintainer: This is what the docs say, but my understanding is
that the finalizer isn't guaranteed to be called, so is this actually
valid?]

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

I would recommend a section on globalization of apps. And here are some
questions that could be added with suggestions on how to answer:

Q: What is a satellite assembly

A: [Discuss how satellite assemblies are used for localizing the app]


Q: How do I generate string resource files for localized versions of my app?

A: [Discuss using resgen to convert XML files to .resources files]


Q: How do I use the string resources I've created?

A: [Discuss the ResourceManager class]


Q: How do I create localized versions of my dialogs

A: [Discuss the Localizable and Language properties of the form as being one
method. This method can require significant work in maintenance for a large
number of languages. I know some people dealing with a large number of
languages in their apps have found other methods that they find easier to
maintain, so some research here might be required]


When I have some more time, I would like to contribute more in terms of
questions AND answers, but I hope this small contribution will be helpful.

Pete

P.S. Keep up the good work.
 
A

Alvin Bruney [MVP]

First of all, after some thought, it occurs to me (and please take this
only
as a constructive comment) that it might be best if the ASP.NET, >ADO.NET and
Windows.Forms sections be removed from the FAQ. Others may >disagree, but it
seems to me that each of these topics is deserving of its own faq (and
some already exist).

That was kinda what i was thinking of when i asked the question
I'm not sure whether i agree though because C# is a base where a user could
come to jump into other related technologies like ado.net. They might not
know of an ado.net newsgroup but they know of this one and it could point
them in the right direction. or equally, maybe it should be a link to the
ado.net faq. not sure here just voicing
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Pete Davis said:
Jon,

Here's what I've got. Hope it's a small help. I'll try to do more when I
have some free time.

First of all, after some thought, it occurs to me (and please take this only
as a constructive comment) that it might be best if the ASP.NET, ADO.NET and
Windows.Forms sections be removed from the FAQ. Others may disagree, but it
seems to me that each of these topics is deserving of its own faq (and some
already exist). I would think the extent that you could discuss these would
be in how they differ in C# from other languages and they don't really, as
they're all part of the framework. Just my opinion and it's no more valid
than anyone else's.

For today I only have one actual question and answer. If I had more time
(and this is a bad week, I don't have much), I would write more. I do have
some suggested questions and some notes on some of them about how they might
be answered:

Q: I have a COM object. Can I use it from C#?

A: There are several ways to use COM objects from C#. As with unmanaged
code, you can do early binding or late binding. Early binding happens at
compile time.

Early binding is faster. It also has the advantage that it's much easier to
use, particuarly in invoking methods, properties and events of a COM object.
However, it also has the limitation that you can only use a compatible
version of the COM object. If the version changes, your C# program will
break.

Late binding is done at runtime and using the ProgID of the COM object, you
can link to whatever version is installed. The disadvantage of late binding
is that it's a bit more difficult to invoke functions.

There are two ways to do early binding. The method I see most often in books
(and to me, it's the more difficult of the two), is to use TlbImp.exe to
create a .NET wrapper assembly (called a Runtime Callable Wrapper or RCW) of
the COM object. An example:

TlbImp mycomob.dll /out:mycomobRCW.dll

This will create a new DLL called mycomboRCW.dll which you can then
reference from your project.

The easier way is from within Visual Studio. Simply go to Add References and
select the COM tab. From here you can choose your COM object and the RCW
will be created automatically behind the scenes without any additional work.

Late binding is done in the following manner. Assuming that mycomob.dll has
an object called MyObjectA, you could use it as follows:

Type myObType = Type.GetTypeFromProgID("MyComOb.MyObjectA");
Object myOb = Activator.CreateInstance(myObType);

Where it gets tricky is invoking methods. This needs to be done with the
InvokeMember method. For example:

object invokeResult = myObType.InvokeMember("MyMethod",
BindingFlags.InvokeMethod,
null,
myOb,
argArray);

Whereas with early binding you could simply do:

MyComOb.MyObjectA myOb = new MyComOb.MyObjectA();
myOb.MyMethod(param1, param2, ...)

While you can manually release COM objects using Marshal.ReleaseComObject()
there is no need. As with any other object in C#, when the object goes out
of scope, the garbage collector will call the Finalizer() which in turn will
call IUnknown::Release() in the COM object.

[note to FAQ maintainer: This is what the docs say, but my understanding is
that the finalizer isn't guaranteed to be called, so is this actually
valid?]

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

I would recommend a section on globalization of apps. And here are some
questions that could be added with suggestions on how to answer:

Q: What is a satellite assembly

A: [Discuss how satellite assemblies are used for localizing the app]


Q: How do I generate string resource files for localized versions of my app?

A: [Discuss using resgen to convert XML files to .resources files]


Q: How do I use the string resources I've created?

A: [Discuss the ResourceManager class]


Q: How do I create localized versions of my dialogs

A: [Discuss the Localizable and Language properties of the form as being one
method. This method can require significant work in maintenance for a large
number of languages. I know some people dealing with a large number of
languages in their apps have found other methods that they find easier to
maintain, so some research here might be required]


When I have some more time, I would like to contribute more in terms of
questions AND answers, but I hope this small contribution will be helpful.

Pete

P.S. Keep up the good work.
 
J

Jon Skeet [C# MVP]

Pete Davis said:
Here's what I've got. Hope it's a small help. I'll try to do more when I
have some free time.

First of all, after some thought, it occurs to me (and please take this only
as a constructive comment) that it might be best if the ASP.NET, ADO.NET and
Windows.Forms sections be removed from the FAQ. Others may disagree, but it
seems to me that each of these topics is deserving of its own faq (and some
already exist). I would think the extent that you could discuss these would
be in how they differ in C# from other languages and they don't really, as
they're all part of the framework. Just my opinion and it's no more valid
than anyone else's.

Well, my aim was to try to get answers for questions which are
frequently asked here. Unfortunately, a lot of questions which should
really be asked on the .adonet, .aspnet or .windowsforms groups are
asked here - so it's worth (IMO) having a section on them. I'm sure
there are more thorough FAQs for those topics, and I hope to include
links to all of them, if people could submit them! (I've got one for
Windows Forms already, along with the Compact Framework FAQ.)
For today I only have one actual question and answer. If I had more time
(and this is a bad week, I don't have much), I would write more. I do have
some suggested questions and some notes on some of them about how they might
be answered:

Goodo - I'll include them as they come, after leaving a couple of days
for discussion. (There are a few other questions on this thread which
haven't been particularly disagreed with, so I'll put them in today.)

[note to FAQ maintainer: This is what the docs say, but my understanding is
that the finalizer isn't guaranteed to be called, so is this actually
valid?]

Finalizers are guaranteed to be called at some indeterminate point
unless the process is exiting and finalizers have already taken too
long. I would hope that the process quitting would do enough to unload
the COM objects anyway, but I'm not in any way an expert on COM.
I would recommend a section on globalization of apps.

Sounds like a good idea - and all of those questions sound like good
ones. Perhaps any questions about Unicode should also be moved to such
a section?
 
J

Jon Skeet [C# MVP]

This is very helpful, many thanks.

I purchased the so-called "Learning Edition" of C# 2003 from Microsoft and
recommend it. It seems to get very little if any mention in these
newsgroups. The book that comes with it is called "Microsoft Visual C# .NET
Step by Step" by John Sharp and Jon Jagger and really does specify every
single step to create all sorts of projects. Because of VAT in this country
C# standard learning edition, including this useful 635 page book is
actually cheaper than C# Standard itself!

Thanks - recommendation duly noted, and included (slightly paraphrased
- let me know if you'd like it changing).
 
D

Daniel O'Connell [C# MVP]

Jon Skeet said:
Pete Davis said:
Here's what I've got. Hope it's a small help. I'll try to do more when I
have some free time.

First of all, after some thought, it occurs to me (and please take this
only
as a constructive comment) that it might be best if the ASP.NET, ADO.NET
and
Windows.Forms sections be removed from the FAQ. Others may disagree, but
it
seems to me that each of these topics is deserving of its own faq (and
some
already exist). I would think the extent that you could discuss these
would
be in how they differ in C# from other languages and they don't really,
as
they're all part of the framework. Just my opinion and it's no more valid
than anyone else's.

Well, my aim was to try to get answers for questions which are
frequently asked here. Unfortunately, a lot of questions which should
really be asked on the .adonet, .aspnet or .windowsforms groups are
asked here - so it's worth (IMO) having a section on them. I'm sure
there are more thorough FAQs for those topics, and I hope to include
links to all of them, if people could submit them! (I've got one for
Windows Forms already, along with the Compact Framework FAQ.)
For today I only have one actual question and answer. If I had more time
(and this is a bad week, I don't have much), I would write more. I do
have
some suggested questions and some notes on some of them about how they
might
be answered:

Goodo - I'll include them as they come, after leaving a couple of days
for discussion. (There are a few other questions on this thread which
haven't been particularly disagreed with, so I'll put them in today.)

[note to FAQ maintainer: This is what the docs say, but my understanding
is
that the finalizer isn't guaranteed to be called, so is this actually
valid?]

Finalizers are guaranteed to be called at some indeterminate point
unless the process is exiting and finalizers have already taken too
long. I would hope that the process quitting would do enough to unload
the COM objects anyway, but I'm not in any way an expert on COM.
I would recommend a section on globalization of apps.

Sounds like a good idea - and all of those questions sound like good
ones. Perhaps any questions about Unicode should also be moved to such
a section?

I wouldn't say moved, maybe just linked. I use unicode quite a bit and I
rarely am invovled in globalization. Unicode on its own is something that
permeates quite a bit(down to the implementation of System.String and why
using a StringWriter can cause odd issues if you specify utf-8 in an xml
document(something to do with encoding, I've never run into it but there are
a few blogs refering to the issue)) and it is something you should
understand, even if you aren't doing globalization or even really using
anything except english and other such characters in ASCII or your default
codepage

For example, unicode character escapes obviously fit best in c# as a
language, the System.Text.Encoding class should be covered in the core
libraries section, etc. Much as it is now. However, some bits like unicode
character escaping would fit well into globalization if presented
differently "How do I specify a string with character X" or hwat have you.
 
J

Jon Skeet [C# MVP]

Daniel O'Connell said:
I wouldn't say moved, maybe just linked. I use unicode quite a bit and I
rarely am invovled in globalization. Unicode on its own is something that
permeates quite a bit(down to the implementation of System.String and why
using a StringWriter can cause odd issues if you specify utf-8 in an xml
document(something to do with encoding, I've never run into it but there are
a few blogs refering to the issue)) and it is something you should
understand, even if you aren't doing globalization or even really using
anything except english and other such characters in ASCII or your default
codepage

For example, unicode character escapes obviously fit best in c# as a
language, the System.Text.Encoding class should be covered in the core
libraries section, etc. Much as it is now. However, some bits like unicode
character escaping would fit well into globalization if presented
differently "How do I specify a string with character X" or hwat have you.

Righto - I think I see what you mean. You can always suggest changes
when it's actually in there :)
 
M

Mattias Sjögren

Pete,
Late binding is done at runtime and using the ProgID of the COM object,

Whether or not you use the ProgID has nothing to do with early/late
binding. You can make early bound calls to an object that you create
from its ProgID (all you need is compile time knowledge of its
interface) and you can make late bound calls to an object created from
the CLSID or in some other way. So these are orthogonal things,
naming/identification and binding.

create a .NET wrapper assembly (called a Runtime Callable Wrapper or RCW) of
the COM object. An example:

TlbImp mycomob.dll /out:mycomobRCW.dll

Now to get really picky, an RCW is not the assembly generated by
TlbImp (that's an interop assembly), but rather an object created from
a class in the interop assembly (such as myOb in your code below).

Type myObType = Type.GetTypeFromProgID("MyComOb.MyObjectA");
Object myOb = Activator.CreateInstance(myObType);




Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top