Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Retrieve name of VB6 calling app via reflection from C# COM interf

Reply
 
Old 04-14-2008   #1 (permalink)
QSIDeveloper


 
 

Retrieve name of VB6 calling app via reflection from C# COM interf

I have a C# logging assembly with a static constructor and methods that is
called from another C# Assembly that is used as a COM interface for a VB6
Application. Ideally I need to build a file name based on the name of the
VB6 application. A second choice would be a file name based on the # COM
interface assembly. I have tried calling Assembly.GetCallingAssembly() but
this fails when I use the VB6 client. Is there a way to get this information
at runtime?

My System SpecsSystem Spec
Old 04-14-2008   #2 (permalink)
amdrit


 
 

Re: Retrieve name of VB6 calling app via reflection from C# COM interf

What do you mean "fails"? Do you get an exception, or do you get and empty
string, or do you get a string that makes no sense?

It would seem to me that you are on the right track, and since the VB Client
is the ultimately the caller, is should come back with it's name if at all.
However, since you have the middle interface, why not require the caller to
name itself to the interface and just pass in App.Name there?


"QSIDeveloper" <QSIDeveloper@xxxxxx> wrote in message
news:8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx
Quote:

>I have a C# logging assembly with a static constructor and methods that is
> called from another C# Assembly that is used as a COM interface for a VB6
> Application. Ideally I need to build a file name based on the name of the
> VB6 application. A second choice would be a file name based on the # COM
> interface assembly. I have tried calling Assembly.GetCallingAssembly()
> but
> this fails when I use the VB6 client. Is there a way to get this
> information
> at runtime?

My System SpecsSystem Spec
Old 04-14-2008   #3 (permalink)
QSIDeveloper


 
 

Re: Retrieve name of VB6 calling app via reflection from C# COM in

When I call GetCallingAssembly It does not fail (my bad) but I get the
Current assembly, in the prev description that would be the C# logging
assembly rather than the C# COM interface assembly as I had expected.

I also tried applying MethodImplAttribute attribute with
MethodImplOptions.NoInlining to the C# logging assembly constructor but it
had no affect.

I also tried GetEntryAssembly but that return null when run with a VB6
executable calling the COM interface.

I would like not to need an additional parameter for the name because this
will impact existing code. I would prefer a silent/automatic name creation.


"amdrit" wrote:
Quote:

> What do you mean "fails"? Do you get an exception, or do you get and empty
> string, or do you get a string that makes no sense?
>
> It would seem to me that you are on the right track, and since the VB Client
> is the ultimately the caller, is should come back with it's name if at all.
> However, since you have the middle interface, why not require the caller to
> name itself to the interface and just pass in App.Name there?
>
>
> "QSIDeveloper" <QSIDeveloper@xxxxxx> wrote in message
> news:8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx
Quote:

> >I have a C# logging assembly with a static constructor and methods that is
> > called from another C# Assembly that is used as a COM interface for a VB6
> > Application. Ideally I need to build a file name based on the name of the
> > VB6 application. A second choice would be a file name based on the # COM
> > interface assembly. I have tried calling Assembly.GetCallingAssembly()
> > but
> > this fails when I use the VB6 client. Is there a way to get this
> > information
> > at runtime?
>
>
>
My System SpecsSystem Spec
Old 04-14-2008   #4 (permalink)
Steven Cheng [MSFT]


 
 

Re: Retrieve name of VB6 calling app via reflection from C# COM in

Hi Amdrit,

As for the "GetCallingAssembly" method, it should return the immediate
caller of the current method. I'm not sure whether there is anything else
in your applicaiton/assmeblies that may cause the problem, but if what you
want to do is get reference to a certain assembly, you can consider using
the a type in the assembly to reference it. e.g.

"MyAssembly.MyType" is a known type defined in the target assemly I want
to get reference
================
Type tp =typeof(MyAssembly.MyType);

Assembly asm = tp.Assembly;

================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
Quote:

>From: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= <QSIDeveloper@xxxxxx>
>References: <8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx>
<OIwt6ClnIHA.1212@xxxxxx>
Quote:

>Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
>Date: Mon, 14 Apr 2008 10:43:14 -0700
Quote:

>
>When I call GetCallingAssembly It does not fail (my bad) but I get the
>Current assembly, in the prev description that would be the C# logging
>assembly rather than the C# COM interface assembly as I had expected.
>
>I also tried applying MethodImplAttribute attribute with
>MethodImplOptions.NoInlining to the C# logging assembly constructor but it
>had no affect.
>
>I also tried GetEntryAssembly but that return null when run with a VB6
>executable calling the COM interface.
>
>I would like not to need an additional parameter for the name because this
>will impact existing code. I would prefer a silent/automatic name
creation.
Quote:

>
>
>"amdrit" wrote:
>
Quote:

>> What do you mean "fails"? Do you get an exception, or do you get and
empty
Quote:
Quote:

>> string, or do you get a string that makes no sense?
>>
>> It would seem to me that you are on the right track, and since the VB
Client
Quote:
Quote:

>> is the ultimately the caller, is should come back with it's name if at
all.
Quote:
Quote:

>> However, since you have the middle interface, why not require the caller
to
Quote:
Quote:

>> name itself to the interface and just pass in App.Name there?
>>
>>
>> "QSIDeveloper" <QSIDeveloper@xxxxxx> wrote in message
>> news:8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx
Quote:

>> >I have a C# logging assembly with a static constructor and methods that
is
Quote:
Quote:
Quote:

>> > called from another C# Assembly that is used as a COM interface for a
VB6
Quote:
Quote:
Quote:

>> > Application. Ideally I need to build a file name based on the name of
the
Quote:
Quote:
Quote:

>> > VB6 application. A second choice would be a file name based on the #
COM
Quote:
Quote:
Quote:

>> > interface assembly. I have tried calling
Assembly.GetCallingAssembly()
Quote:
Quote:
Quote:

>> > but
>> > this fails when I use the VB6 client. Is there a way to get this
>> > information
>> > at runtime?
>>
>>
>>
>
My System SpecsSystem Spec
Old 04-15-2008   #5 (permalink)
QSIDeveloper


 
 

Re: Retrieve name of VB6 calling app via reflection from C# COM in

In the real app I don't know hat the calling assembly is nor do I know ablut
any types it contains.

Here is a simple example of the issue.
// Calling assembly
namespace ReflectionTest
{
class Program
{
static void Main(string[] args)
{
MyLogger.Logger.LogInfo("Hello");
}
}
}

// logger
namespace MyLogger
{
public class Logger
{
static string callingAssembly = string.Empty;
static Logger()
{

// This is where I need to know who called. The first call is
to LogInfo Below
// that causes this constructor to be called.
// What I need to know is the assembly that called LogInfo
string caller = Assembly.GetCallingAssembly().FullName; // this
returns 'Logger'
string s;
Assembly a = Assembly.GetEntryAssembly();
if (null != a)
s = a.FullName; // this gives me 'ReflectionTester', but
when called from VB6. a is null in that case
}

public static void LogInfo(string info)
{
if (callingAssembly.Equals(string.Empty))
callingAssembly = Assembly.GetCallingAssembly().FullName;
//This gives 'Logger' but it is too late by the time this is run

}
}
}

"Steven Cheng [MSFT]" wrote:
Quote:

> Hi Amdrit,
>
> As for the "GetCallingAssembly" method, it should return the immediate
> caller of the current method. I'm not sure whether there is anything else
> in your applicaiton/assmeblies that may cause the problem, but if what you
> want to do is get reference to a certain assembly, you can consider using
> the a type in the assembly to reference it. e.g.
>
> "MyAssembly.MyType" is a known type defined in the target assemly I want
> to get reference
> ================
> Type tp =typeof(MyAssembly.MyType);
>
> Assembly asm = tp.Assembly;
>
> ================
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@xxxxxx.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscripti...ult.aspx#notif
> ications.
>
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> --------------------
Quote:

> >From: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= <QSIDeveloper@xxxxxx>
> >References: <8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx>
> <OIwt6ClnIHA.1212@xxxxxx>
Quote:

> >Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
> >Date: Mon, 14 Apr 2008 10:43:14 -0700
>
Quote:

> >
> >When I call GetCallingAssembly It does not fail (my bad) but I get the
> >Current assembly, in the prev description that would be the C# logging
> >assembly rather than the C# COM interface assembly as I had expected.
> >
> >I also tried applying MethodImplAttribute attribute with
> >MethodImplOptions.NoInlining to the C# logging assembly constructor but it
> >had no affect.
> >
> >I also tried GetEntryAssembly but that return null when run with a VB6
> >executable calling the COM interface.
> >
> >I would like not to need an additional parameter for the name because this
> >will impact existing code. I would prefer a silent/automatic name
> creation.
Quote:

> >
> >
> >"amdrit" wrote:
> >
Quote:

> >> What do you mean "fails"? Do you get an exception, or do you get and
> empty
Quote:
Quote:

> >> string, or do you get a string that makes no sense?
> >>
> >> It would seem to me that you are on the right track, and since the VB
> Client
Quote:
Quote:

> >> is the ultimately the caller, is should come back with it's name if at
> all.
Quote:
Quote:

> >> However, since you have the middle interface, why not require the caller
> to
Quote:
Quote:

> >> name itself to the interface and just pass in App.Name there?
> >>
> >>
> >> "QSIDeveloper" <QSIDeveloper@xxxxxx> wrote in message
> >> news:8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx
> >> >I have a C# logging assembly with a static constructor and methods that
> is
Quote:
Quote:

> >> > called from another C# Assembly that is used as a COM interface for a
> VB6
Quote:
Quote:

> >> > Application. Ideally I need to build a file name based on the name of
> the
Quote:
Quote:

> >> > VB6 application. A second choice would be a file name based on the #
> COM
Quote:
Quote:

> >> > interface assembly. I have tried calling
> Assembly.GetCallingAssembly()
Quote:
Quote:

> >> > but
> >> > this fails when I use the VB6 client. Is there a way to get this
> >> > information
> >> > at runtime?
> >>
> >>
> >>
> >
>
>
My System SpecsSystem Spec
Old 04-16-2008   #6 (permalink)
Steven Cheng [MSFT]


 
 

Re: Retrieve name of VB6 calling app via reflection from C# COM in

Thanks for your reply Amdrit,

Based on the complete code sample you provided, I've also tested it on my
side. I think the output is as expected.

In the static constructor of "Logger" class, the "GetCallingAssembly" will
return "MyLogger" assembly because the static constructor is called by .NET
runtime's internal code which is marked as the assembly itself(Rather than
the "ReflectionTest" app assembly).

And in the "LogInfo" method, the "GetCallingAssembly" will return
"ReflectionTest" because it is the immediate caller of the LogInfo method.
Also, if you call "GetEntryAssembly" it will return the assembly that
contains the Main entry point, that's the "ReflectionTest" app assembly
here. You can also see this chain correctly via the CallStack in
debugger's callstack window.

However, for unmanaged VB6 client, it is not a managed application, the
.NET runtime won't mark it as part of the calling assembly chain or entry
point, that's why you will not get it as Entry point assembly.

Why do you need to cache the calling AssemblyName in static constructor? I
think at runtime, it is possible that many different classes in different
assemblies may call "LogInfo" method and the "CallingAssmebly" may also
vary from time to time. It is reasonable to call "GetCallingAssembly" in
"LogInfo" method if you do need to get the assembly who invoke that method.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Quote:

>Thread-Topic: Retrieve name of VB6 calling app via reflection from C# COM
in
Quote:

>thread-index: Acie74kZBFIskDvWSkqeSsNJrp7vVQ==
>Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
>Date: Tue, 15 Apr 2008 04:55:01 -0700
Quote:

>In the real app I don't know hat the calling assembly is nor do I know
ablut
Quote:

>any types it contains.
>
>Here is a simple example of the issue.
>// Calling assembly
>namespace ReflectionTest
>{
> class Program
> {
> static void Main(string[] args)
> {
> MyLogger.Logger.LogInfo("Hello");
> }
> }
>}
>
>// logger
>namespace MyLogger
>{
> public class Logger
> {
> static string callingAssembly = string.Empty;
> static Logger()
> {
>
> // This is where I need to know who called. The first call is
>to LogInfo Below
> // that causes this constructor to be called.
> // What I need to know is the assembly that called LogInfo
> string caller = Assembly.GetCallingAssembly().FullName; //
this
Quote:

>returns 'Logger'
> string s;
> Assembly a = Assembly.GetEntryAssembly();
> if (null != a)
> s = a.FullName; // this gives me 'ReflectionTester', but
>when called from VB6. a is null in that case
> }
>
> public static void LogInfo(string info)
> {
> if (callingAssembly.Equals(string.Empty))
> callingAssembly = Assembly.GetCallingAssembly().FullName;
>//This gives 'Logger' but it is too late by the time this is run
>
> }
> }
>}
>
>"Steven Cheng [MSFT]" wrote:
>
Quote:

>> Hi Amdrit,
>>
>> As for the "GetCallingAssembly" method, it should return the immediate
>> caller of the current method. I'm not sure whether there is anything
else
Quote:
Quote:

>> in your applicaiton/assmeblies that may cause the problem, but if what
you
Quote:
Quote:

>> want to do is get reference to a certain assembly, you can consider
using
Quote:
Quote:

>> the a type in the assembly to reference it. e.g.
>>
>> "MyAssembly.MyType" is a known type defined in the target assemly I
want
Quote:
Quote:

>> to get reference
>> ================
>> Type tp =typeof(MyAssembly.MyType);
>>
>> Assembly asm = tp.Assembly;
>>
>> ================
>>
>> Sincerely,
>>
>> Steven Cheng
>>
>> Microsoft MSDN Online Support Lead
>>
>>
>> Delighting our customers is our #1 priority. We welcome your comments
and
Quote:
Quote:

>> suggestions about how we can improve the support we provide to you.
Please
Quote:
Quote:

>> feel free to let my manager know what you think of the level of service
>> provided. You can send feedback directly to my manager at:
>> msdnmg@xxxxxx.
>>
>> ==================================================
>> Get notification to my posts through email? Please refer to
>>
http://msdn.microsoft.com/subscripti...ult.aspx#notif
Quote:
Quote:

>> ications.
>>
>> ==================================================
>> This posting is provided "AS IS" with no warranties, and confers no
rights.
Quote:
Quote:

>>
>>
>> --------------------
Quote:

>> >From: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= <QSIDeveloper@xxxxxx>
>> >References: <8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx>
>> <OIwt6ClnIHA.1212@xxxxxx>
Quote:

>> >Subject: Re: Retrieve name of VB6 calling app via reflection from C#
COM in
Quote:
Quote:
Quote:

>> >Date: Mon, 14 Apr 2008 10:43:14 -0700
>>
Quote:

>> >
>> >When I call GetCallingAssembly It does not fail (my bad) but I get the
>> >Current assembly, in the prev description that would be the C# logging
>> >assembly rather than the C# COM interface assembly as I had expected.
>> >
>> >I also tried applying MethodImplAttribute attribute with
>> >MethodImplOptions.NoInlining to the C# logging assembly constructor but
it
Quote:
Quote:
Quote:

>> >had no affect.
>> >
>> >I also tried GetEntryAssembly but that return null when run with a VB6
>> >executable calling the COM interface.
>> >
>> >I would like not to need an additional parameter for the name because
this
Quote:
Quote:
Quote:

>> >will impact existing code. I would prefer a silent/automatic name
>> creation.
Quote:

>> >
>> >
>> >"amdrit" wrote:
>> >
>> >> What do you mean "fails"? Do you get an exception, or do you get and
>> empty
Quote:

>> >> string, or do you get a string that makes no sense?
>> >>
>> >> It would seem to me that you are on the right track, and since the VB
>> Client
Quote:

>> >> is the ultimately the caller, is should come back with it's name if
at
Quote:
Quote:

>> all.
Quote:

>> >> However, since you have the middle interface, why not require the
caller
Quote:
Quote:

>> to
Quote:

>> >> name itself to the interface and just pass in App.Name there?
>> >>
>> >>
>> >> "QSIDeveloper" <QSIDeveloper@xxxxxx> wrote in message
>> >> news:8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx
>> >> >I have a C# logging assembly with a static constructor and methods
that
Quote:
Quote:

>> is
Quote:

>> >> > called from another C# Assembly that is used as a COM interface for
a
Quote:
Quote:

>> VB6
Quote:

>> >> > Application. Ideally I need to build a file name based on the name
of
Quote:
Quote:

>> the
Quote:

>> >> > VB6 application. A second choice would be a file name based on the
#
Quote:
Quote:

>> COM
Quote:

>> >> > interface assembly. I have tried calling
>> Assembly.GetCallingAssembly()
Quote:

>> >> > but
>> >> > this fails when I use the VB6 client. Is there a way to get this
>> >> > information
>> >> > at runtime?
>> >>
>> >>
>> >>
>> >
>>
>>
>
My System SpecsSystem Spec
Old 04-16-2008   #7 (permalink)
QSIDeveloper


 
 

Re: Retrieve name of VB6 calling app via reflection from C# COM in

Thank you for looking at this, I had hoped I had overlooked something.
In most cases the logger is consumed by a .NET executable. In that case the
config information is found in the app.config file. In this case the main
executable is a VB6 application we that will eventually be replaced. New
feature are being added using .NET via COM. So as you know normally the
config file name is related to the exe as assembly.exe.config. In the case
of the VB6 EXE, the COM interface will be the only consumer of the logger so
I am looking for a way to build a config file name based on the assembly name
of the COM interface assembly rather than the usual exe. I was hoping there
was some way through reflection to get the assembly name (GetCallingAssembly
of CallingAssembly ) in case we need to have another similar situation with
another interface to legacy code rather than hard coding or passing in a
name.

Is there some way at run time to get the stack information?
If not thanks for you help.


"Steven Cheng [MSFT]" wrote:
Quote:

> Thanks for your reply Amdrit,
>
> Based on the complete code sample you provided, I've also tested it on my
> side. I think the output is as expected.
>
> In the static constructor of "Logger" class, the "GetCallingAssembly" will
> return "MyLogger" assembly because the static constructor is called by .NET
> runtime's internal code which is marked as the assembly itself(Rather than
> the "ReflectionTest" app assembly).
>
> And in the "LogInfo" method, the "GetCallingAssembly" will return
> "ReflectionTest" because it is the immediate caller of the LogInfo method.
> Also, if you call "GetEntryAssembly" it will return the assembly that
> contains the Main entry point, that's the "ReflectionTest" app assembly
> here. You can also see this chain correctly via the CallStack in
> debugger's callstack window.
>
> However, for unmanaged VB6 client, it is not a managed application, the
> .NET runtime won't mark it as part of the calling assembly chain or entry
> point, that's why you will not get it as Entry point assembly.
>
> Why do you need to cache the calling AssemblyName in static constructor? I
> think at runtime, it is possible that many different classes in different
> assemblies may call "LogInfo" method and the "CallingAssmebly" may also
> vary from time to time. It is reasonable to call "GetCallingAssembly" in
> "LogInfo" method if you do need to get the assembly who invoke that method.
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@xxxxxx.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscripti...ult.aspx#notif
> ications.
>
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
> --------------------
Quote:

> >Thread-Topic: Retrieve name of VB6 calling app via reflection from C# COM
> in
Quote:

> >thread-index: Acie74kZBFIskDvWSkqeSsNJrp7vVQ==
> >Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
> >Date: Tue, 15 Apr 2008 04:55:01 -0700
>
Quote:

> >In the real app I don't know hat the calling assembly is nor do I know
> ablut
Quote:

> >any types it contains.
> >
> >Here is a simple example of the issue.
> >// Calling assembly
> >namespace ReflectionTest
> >{
> > class Program
> > {
> > static void Main(string[] args)
> > {
> > MyLogger.Logger.LogInfo("Hello");
> > }
> > }
> >}
> >
> >// logger
> >namespace MyLogger
> >{
> > public class Logger
> > {
> > static string callingAssembly = string.Empty;
> > static Logger()
> > {
> >
> > // This is where I need to know who called. The first call is
> >to LogInfo Below
> > // that causes this constructor to be called.
> > // What I need to know is the assembly that called LogInfo
> > string caller = Assembly.GetCallingAssembly().FullName; //
> this
Quote:

> >returns 'Logger'
> > string s;
> > Assembly a = Assembly.GetEntryAssembly();
> > if (null != a)
> > s = a.FullName; // this gives me 'ReflectionTester', but
> >when called from VB6. a is null in that case
> > }
> >
> > public static void LogInfo(string info)
> > {
> > if (callingAssembly.Equals(string.Empty))
> > callingAssembly = Assembly.GetCallingAssembly().FullName;
> >//This gives 'Logger' but it is too late by the time this is run
> >
> > }
> > }
> >}
> >
> >"Steven Cheng [MSFT]" wrote:
> >
Quote:

> >> Hi Amdrit,
> >>
> >> As for the "GetCallingAssembly" method, it should return the immediate
> >> caller of the current method. I'm not sure whether there is anything
> else
Quote:
Quote:

> >> in your applicaiton/assmeblies that may cause the problem, but if what
> you
Quote:
Quote:

> >> want to do is get reference to a certain assembly, you can consider
> using
Quote:
Quote:

> >> the a type in the assembly to reference it. e.g.
> >>
> >> "MyAssembly.MyType" is a known type defined in the target assemly I
> want
Quote:
Quote:

> >> to get reference
> >> ================
> >> Type tp =typeof(MyAssembly.MyType);
> >>
> >> Assembly asm = tp.Assembly;
> >>
> >> ================
> >>
> >> Sincerely,
> >>
> >> Steven Cheng
> >>
> >> Microsoft MSDN Online Support Lead
> >>
> >>
> >> Delighting our customers is our #1 priority. We welcome your comments
> and
Quote:
Quote:

> >> suggestions about how we can improve the support we provide to you.
> Please
Quote:
Quote:

> >> feel free to let my manager know what you think of the level of service
> >> provided. You can send feedback directly to my manager at:
> >> msdnmg@xxxxxx.
> >>
> >> ==================================================
> >> Get notification to my posts through email? Please refer to
> >>
> http://msdn.microsoft.com/subscripti...ult.aspx#notif
Quote:
Quote:

> >> ications.
> >>
> >> ==================================================
> >> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Quote:
Quote:

> >>
> >>
> >> --------------------
> >> >From: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= <QSIDeveloper@xxxxxx>
> >> >References: <8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx>
> >> <OIwt6ClnIHA.1212@xxxxxx>
> >> >Subject: Re: Retrieve name of VB6 calling app via reflection from C#
> COM in
Quote:
Quote:

> >> >Date: Mon, 14 Apr 2008 10:43:14 -0700
> >>
> >> >
> >> >When I call GetCallingAssembly It does not fail (my bad) but I get the
> >> >Current assembly, in the prev description that would be the C# logging
> >> >assembly rather than the C# COM interface assembly as I had expected.
> >> >
> >> >I also tried applying MethodImplAttribute attribute with
> >> >MethodImplOptions.NoInlining to the C# logging assembly constructor but
> it
Quote:
Quote:

> >> >had no affect.
> >> >
> >> >I also tried GetEntryAssembly but that return null when run with a VB6
> >> >executable calling the COM interface.
> >> >
> >> >I would like not to need an additional parameter for the name because
> this
Quote:
Quote:

> >> >will impact existing code. I would prefer a silent/automatic name
> >> creation.
> >> >
> >> >
> >> >"amdrit" wrote:
> >> >
> >> >> What do you mean "fails"? Do you get an exception, or do you get and
> >> empty
> >> >> string, or do you get a string that makes no sense?
> >> >>
> >> >> It would seem to me that you are on the right track, and since the VB
> >> Client
> >> >> is the ultimately the caller, is should come back with it's name if
> at
Quote:
Quote:

> >> all.
> >> >> However, since you have the middle interface, why not require the
> caller
Quote:
Quote:

> >> to
> >> >> name itself to the interface and just pass in App.Name there?
> >> >>
> >> >>
> >> >> "QSIDeveloper" <QSIDeveloper@xxxxxx> wrote in message
> >> >> news:8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx
> >> >> >I have a C# logging assembly with a static constructor and methods
> that
Quote:
Quote:

> >> is
> >> >> > called from another C# Assembly that is used as a COM interface for
> a
Quote:
Quote:

> >> VB6
> >> >> > Application. Ideally I need to build a file name based on the name
> of
Quote:
Quote:

> >> the
> >> >> > VB6 application. A second choice would be a file name based on the
> #
Quote:
Quote:

> >> COM
> >> >> > interface assembly. I have tried calling
> >> Assembly.GetCallingAssembly()
> >> >> > but
> >> >> > this fails when I use the VB6 client. Is there a way to get this
> >> >> > information
> >> >> > at runtime?
> >> >>
> >> >>
> >> >>
> >> >
> >>
> >>
> >
>
>
My System SpecsSystem Spec
Old 04-17-2008   #8 (permalink)
Steven Cheng [MSFT]


 
 

Re: Retrieve name of VB6 calling app via reflection from C# COM in

Thanks for your reply Amdrit,

There does exist a StackTrace class under "System.Diagnositcs" namespace
which can help capture the callstack(for managed code) of the current
thread.

#StackTrace Class
http://msdn2.microsoft.com/en-us/lib...acktrace(VS.71
).aspx

BTW, for unmanaged application(use COM interop to call .net component),it
can also have a exe.config file and you can store some simple data such as
<appSettings> items in it. Not sure whether this will be helpful?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Quote:

>Thread-Topic: Retrieve name of VB6 calling app via reflection from C# COM i
>From: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= <QSIDeveloper@xxxxxx>
>References: <8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx>
<OIwt6ClnIHA.1212@xxxxxx>
<77B1C1FB-649F-4D68-B9A0-482E6D80C98A@xxxxxx>
<pKF1ZnqnIHA.9016@xxxxxx>
<B9A38563-A15F-4C26-8A07-CE4B40A1CE42@xxxxxx>
<Rmir6d5nIHA.9932@xxxxxx>
Quote:

>Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
>Date: Wed, 16 Apr 2008 04:18:00 -0700
Quote:

>
>Thank you for looking at this, I had hoped I had overlooked something.
>In most cases the logger is consumed by a .NET executable. In that case
the
Quote:

>config information is found in the app.config file. In this case the main
>executable is a VB6 application we that will eventually be replaced. New
>feature are being added using .NET via COM. So as you know normally the
>config file name is related to the exe as assembly.exe.config. In the
case
Quote:

>of the VB6 EXE, the COM interface will be the only consumer of the logger
so
Quote:

>I am looking for a way to build a config file name based on the assembly
name
Quote:

>of the COM interface assembly rather than the usual exe. I was hoping
there
Quote:

>was some way through reflection to get the assembly name
(GetCallingAssembly
Quote:

>of CallingAssembly ) in case we need to have another similar situation
with
Quote:

>another interface to legacy code rather than hard coding or passing in a
>name.
>
>Is there some way at run time to get the stack information?
>If not thanks for you help.
>
>
>"Steven Cheng [MSFT]" wrote:
>
Quote:

>> Thanks for your reply Amdrit,
>>
>> Based on the complete code sample you provided, I've also tested it on
my
Quote:
Quote:

>> side. I think the output is as expected.
>>
>> In the static constructor of "Logger" class, the "GetCallingAssembly"
will
Quote:
Quote:

>> return "MyLogger" assembly because the static constructor is called by
.NET
Quote:
Quote:

>> runtime's internal code which is marked as the assembly itself(Rather
than
Quote:
Quote:

>> the "ReflectionTest" app assembly).
>>
>> And in the "LogInfo" method, the "GetCallingAssembly" will return
>> "ReflectionTest" because it is the immediate caller of the LogInfo
method.
Quote:
Quote:

>> Also, if you call "GetEntryAssembly" it will return the assembly that
>> contains the Main entry point, that's the "ReflectionTest" app assembly
>> here. You can also see this chain correctly via the CallStack in
>> debugger's callstack window.
>>
>> However, for unmanaged VB6 client, it is not a managed application, the
>> .NET runtime won't mark it as part of the calling assembly chain or
entry
Quote:
Quote:

>> point, that's why you will not get it as Entry point assembly.
>>
>> Why do you need to cache the calling AssemblyName in static
constructor? I
Quote:
Quote:

>> think at runtime, it is possible that many different classes in
different
Quote:
Quote:

>> assemblies may call "LogInfo" method and the "CallingAssmebly" may also
>> vary from time to time. It is reasonable to call "GetCallingAssembly" in
>> "LogInfo" method if you do need to get the assembly who invoke that
method.
Quote:
Quote:

>>
>> Sincerely,
>>
>> Steven Cheng
>>
>> Microsoft MSDN Online Support Lead
>>
>>
>> Delighting our customers is our #1 priority. We welcome your comments
and
Quote:
Quote:

>> suggestions about how we can improve the support we provide to you.
Please
Quote:
Quote:

>> feel free to let my manager know what you think of the level of service
>> provided. You can send feedback directly to my manager at:
>> msdnmg@xxxxxx.
>>
>> ==================================================
>> Get notification to my posts through email? Please refer to
>>
http://msdn.microsoft.com/subscripti...ult.aspx#notif
Quote:
Quote:

>> ications.
>>
>> ==================================================
>> This posting is provided "AS IS" with no warranties, and confers no
rights.
Quote:
Quote:

>> --------------------
Quote:

>> >Thread-Topic: Retrieve name of VB6 calling app via reflection from C#
COM
Quote:
Quote:

>> in
Quote:

>> >thread-index: Acie74kZBFIskDvWSkqeSsNJrp7vVQ==
>> >Subject: Re: Retrieve name of VB6 calling app via reflection from C#
COM in
Quote:
Quote:
Quote:

>> >Date: Tue, 15 Apr 2008 04:55:01 -0700
>>
Quote:

>> >In the real app I don't know hat the calling assembly is nor do I know
>> ablut
Quote:

>> >any types it contains.
>> >
>> >Here is a simple example of the issue.
>> >// Calling assembly
>> >namespace ReflectionTest
>> >{
>> > class Program
>> > {
>> > static void Main(string[] args)
>> > {
>> > MyLogger.Logger.LogInfo("Hello");
>> > }
>> > }
>> >}
>> >
>> >// logger
>> >namespace MyLogger
>> >{
>> > public class Logger
>> > {
>> > static string callingAssembly = string.Empty;
>> > static Logger()
>> > {
>> >
>> > // This is where I need to know who called. The first call
is
Quote:
Quote:
Quote:

>> >to LogInfo Below
>> > // that causes this constructor to be called.
>> > // What I need to know is the assembly that called LogInfo
>> > string caller = Assembly.GetCallingAssembly().FullName; //
>> this
Quote:

>> >returns 'Logger'
>> > string s;
>> > Assembly a = Assembly.GetEntryAssembly();
>> > if (null != a)
>> > s = a.FullName; // this gives me 'ReflectionTester',
but
Quote:
Quote:
Quote:

>> >when called from VB6. a is null in that case
>> > }
>> >
>> > public static void LogInfo(string info)
>> > {
>> > if (callingAssembly.Equals(string.Empty))
>> > callingAssembly =
Assembly.GetCallingAssembly().FullName;
Quote:
Quote:
Quote:

>> >//This gives 'Logger' but it is too late by the time this is run
>> >
>> > }
>> > }
>> >}
>> >
>> >"Steven Cheng [MSFT]" wrote:
>> >
>> >> Hi Amdrit,
>> >>
>> >> As for the "GetCallingAssembly" method, it should return the
immediate
Quote:
Quote:
Quote:

>> >> caller of the current method. I'm not sure whether there is anything
>> else
Quote:

>> >> in your applicaiton/assmeblies that may cause the problem, but if
what
Quote:
Quote:

>> you
Quote:

>> >> want to do is get reference to a certain assembly, you can consider
>> using
Quote:

>> >> the a type in the assembly to reference it. e.g.
>> >>
>> >> "MyAssembly.MyType" is a known type defined in the target assemly I
>> want
Quote:

>> >> to get reference
>> >> ================
>> >> Type tp =typeof(MyAssembly.MyType);
>> >>
>> >> Assembly asm = tp.Assembly;
>> >>
>> >> ================
>> >>
>> >> Sincerely,
>> >>
>> >> Steven Cheng
>> >>
>> >> Microsoft MSDN Online Support Lead
>> >>
>> >>
>> >> Delighting our customers is our #1 priority. We welcome your comments
>> and
Quote:

>> >> suggestions about how we can improve the support we provide to you.
>> Please
Quote:

>> >> feel free to let my manager know what you think of the level of
service
Quote:
Quote:
Quote:

>> >> provided. You can send feedback directly to my manager at:
>> >> msdnmg@xxxxxx.
>> >>
>> >> ==================================================
>> >> Get notification to my posts through email? Please refer to
>> >>
>>
http://msdn.microsoft.com/subscripti...ult.aspx#notif
Quote:
Quote:
Quote:

>> >> ications.
>> >>
>> >> ==================================================
>> >> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
Quote:

>> >>
>> >>
>> >> --------------------
>> >> >From: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= <QSIDeveloper@xxxxxx>
>> >> >References: <8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx>
>> >> <OIwt6ClnIHA.1212@xxxxxx>
>> >> >Subject: Re: Retrieve name of VB6 calling app via reflection from C#
>> COM in
Quote:

>> >> >Date: Mon, 14 Apr 2008 10:43:14 -0700
>> >>
>> >> >
>> >> >When I call GetCallingAssembly It does not fail (my bad) but I get
the
Quote:
Quote:
Quote:

>> >> >Current assembly, in the prev description that would be the C#
logging
Quote:
Quote:
Quote:

>> >> >assembly rather than the C# COM interface assembly as I had expected.
>> >> >
>> >> >I also tried applying MethodImplAttribute attribute with
>> >> >MethodImplOptions.NoInlining to the C# logging assembly constructor
but
Quote:
Quote:

>> it
Quote:

>> >> >had no affect.
>> >> >
>> >> >I also tried GetEntryAssembly but that return null when run with a
VB6
Quote:
Quote:
Quote:

>> >> >executable calling the COM interface.
>> >> >
>> >> >I would like not to need an additional parameter for the name
because
Quote:
Quote:

>> this
Quote:

>> >> >will impact existing code. I would prefer a silent/automatic name
>> >> creation.
>> >> >
>> >> >
>> >> >"amdrit" wrote:
>> >> >
>> >> >> What do you mean "fails"? Do you get an exception, or do you get
and
Quote:
Quote:
Quote:

>> >> empty
>> >> >> string, or do you get a string that makes no sense?
>> >> >>
>> >> >> It would seem to me that you are on the right track, and since the
VB
Quote:
Quote:
Quote:

>> >> Client
>> >> >> is the ultimately the caller, is should come back with it's name
if
Quote:
Quote:

>> at
Quote:

>> >> all.
>> >> >> However, since you have the middle interface, why not require the
>> caller
Quote:

>> >> to
>> >> >> name itself to the interface and just pass in App.Name there?
>> >> >>
>> >> >>
>> >> >> "QSIDeveloper" <QSIDeveloper@xxxxxx> wrote in message
>> >> >> news:8A940661-D84F-4602-B1E5-5477864E6962@xxxxxx
>> >> >> >I have a C# logging assembly with a static constructor and
methods
Quote:
Quote:

>> that
Quote:

>> >> is
>> >> >> > called from another C# Assembly that is used as a COM interface
for
Quote:
Quote:

>> a
Quote:

>> >> VB6
>> >> >> > Application. Ideally I need to build a file name based on the
name
Quote:
Quote:

>> of
Quote:

>> >> the
>> >> >> > VB6 application. A second choice would be a file name based on
the
Quote:
Quote:

>> #
Quote:

>> >> COM
>> >> >> > interface assembly. I have tried calling
>> >> Assembly.GetCallingAssembly()
>> >> >> > but
>> >> >> > this fails when I use the VB6 client. Is there a way to get
this
Quote:
Quote:
Quote:

>> >> >> > information
>> >> >> > at runtime?
>> >> >>
>> >> >>
>> >> >>
>> >> >
>> >>
>> >>
>> >
>>
>>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to retrieve a generic method with GetMethod (reflection) ? PowerShell
Calling List<T>.AddRange with Reflection.Emit .NET General
Question on Reflection .NET General
reflection, COM, and Vista .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46