473,695 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetCal lingAssembly() but
this fails when I use the VB6 client. Is there a way to get this information
at runtime?
Jun 27 '08 #1
7 2682
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?
"QSIDevelop er" <QS**********@n ewsgroup.nospam wrote in message
news:8A******** *************** ***********@mic rosoft.com...
>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.GetCal lingAssembly()
but
this fails when I use the VB6 client. Is there a way to get this
information
at runtime?

Jun 27 '08 #2
When I call GetCallingAssem bly 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 MethodImplAttri bute attribute with
MethodImplOptio ns.NoInlining to the C# logging assembly constructor but it
had no affect.

I also tried GetEntryAssembl y 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:
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?
"QSIDevelop er" <QS**********@n ewsgroup.nospam wrote in message
news:8A******** *************** ***********@mic rosoft.com...
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.GetCal lingAssembly()
but
this fails when I use the VB6 client. Is there a way to get this
information
at runtime?


Jun 27 '08 #3
Hi Amdrit,

As for the "GetCallingAsse mbly" 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.MyT ype" is a known type defined in the target assemly I want
to get reference
=============== =
Type tp =typeof(MyAssem bly.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:
ms****@microsof t.com.

=============== =============== =============== =====
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.
--------------------
>From: =?Utf-8?B?UVNJRGV2ZWx vcGVy?= <QS**********@n ewsgroup.nospam >
References: <8A************ *************** *******@microso ft.com>
<OI************ **@TK2MSFTNGP05 .phx.gbl>
>Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
Date: Mon, 14 Apr 2008 10:43:14 -0700
>
When I call GetCallingAssem bly 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 MethodImplAttri bute attribute with
MethodImplOpti ons.NoInlining to the C# logging assembly constructor but it
had no affect.

I also tried GetEntryAssembl y 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:
>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?
"QSIDevelope r" <QS**********@n ewsgroup.nospam wrote in message
news:8A******* *************** ************@mi crosoft.com...
>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.GetCal lingAssembly()
but
this fails when I use the VB6 client. Is there a way to get this
information
at runtime?


Jun 27 '08 #4
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.GetCal lingAssembly(). FullName; // this
returns 'Logger'
string s;
Assembly a = Assembly.GetEnt ryAssembly();
if (null != a)
s = a.FullName; // this gives me 'ReflectionTest er', but
when called from VB6. a is null in that case
}

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

}
}
}

"Steven Cheng [MSFT]" wrote:
Hi Amdrit,

As for the "GetCallingAsse mbly" 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.MyT ype" is a known type defined in the target assemly I want
to get reference
=============== =
Type tp =typeof(MyAssem bly.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:
ms****@microsof t.com.

=============== =============== =============== =====
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.
--------------------
From: =?Utf-8?B?UVNJRGV2ZWx vcGVy?= <QS**********@n ewsgroup.nospam >
References: <8A************ *************** *******@microso ft.com>
<OI************ **@TK2MSFTNGP05 .phx.gbl>
Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
Date: Mon, 14 Apr 2008 10:43:14 -0700

When I call GetCallingAssem bly 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 MethodImplAttri bute attribute with
MethodImplOptio ns.NoInlining to the C# logging assembly constructor but it
had no affect.

I also tried GetEntryAssembl y 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:
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?
"QSIDevelop er" <QS**********@n ewsgroup.nospam wrote in message
news:8A******** *************** ***********@mic rosoft.com...
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.GetCal lingAssembly()
but
this fails when I use the VB6 client. Is there a way to get this
information
at runtime?

Jun 27 '08 #5
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 "GetCallingAsse mbly" 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 "GetCallingAsse mbly" will return
"ReflectionTest " because it is the immediate caller of the LogInfo method.
Also, if you call "GetEntryAssemb ly" 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 "CallingAssmebl y" may also
vary from time to time. It is reasonable to call "GetCallingAsse mbly" 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:
ms****@microsof t.com.

=============== =============== =============== =====
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.
--------------------
>Thread-Topic: Retrieve name of VB6 calling app via reflection from C# COM
in
>thread-index: Acie74kZBFIskDv WSkqeSsNJrp7vVQ ==
Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
Date: Tue, 15 Apr 2008 04:55:01 -0700
>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.GetCal lingAssembly(). FullName; //
this
>returns 'Logger'
string s;
Assembly a = Assembly.GetEnt ryAssembly();
if (null != a)
s = a.FullName; // this gives me 'ReflectionTest er', but
when called from VB6. a is null in that case
}

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

}
}
}

"Steven Cheng [MSFT]" wrote:
>Hi Amdrit,

As for the "GetCallingAsse mbly" 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.My Type" is a known type defined in the target assemly I
want
>to get reference
============== ==
Type tp =typeof(MyAssem bly.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:
ms****@microsof t.com.

============== =============== =============== ======
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.
>>

--------------------
>From: =?Utf-8?B?UVNJRGV2ZWx vcGVy?= <QS**********@n ewsgroup.nospam >
References: <8A************ *************** *******@microso ft.com>
<OI*********** ***@TK2MSFTNGP0 5.phx.gbl>
>Subject: Re: Retrieve name of VB6 calling app via reflection from C#
COM in
>Date: Mon, 14 Apr 2008 10:43:14 -0700
>
When I call GetCallingAssem bly 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 MethodImplAttri bute attribute with
MethodImplOpti ons.NoInlining to the C# logging assembly constructor but
it
>had no affect.

I also tried GetEntryAssembl y 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:

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?
"QSIDevelope r" <QS**********@n ewsgroup.nospam wrote in message
news:8A******* *************** ************@mi crosoft.com...
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.GetCa llingAssembly()
but
this fails when I use the VB6 client. Is there a way to get this
information
at runtime?


Jun 27 '08 #6
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.co nfig. 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 (GetCallingAsse mbly
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:
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 "GetCallingAsse mbly" 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 "GetCallingAsse mbly" will return
"ReflectionTest " because it is the immediate caller of the LogInfo method.
Also, if you call "GetEntryAssemb ly" 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 "CallingAssmebl y" may also
vary from time to time. It is reasonable to call "GetCallingAsse mbly" 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:
ms****@microsof t.com.

=============== =============== =============== =====
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.
--------------------
Thread-Topic: Retrieve name of VB6 calling app via reflection from C# COM
in
thread-index: Acie74kZBFIskDv WSkqeSsNJrp7vVQ ==
Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
Date: Tue, 15 Apr 2008 04:55:01 -0700
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.GetCal lingAssembly(). FullName; //
this
returns 'Logger'
string s;
Assembly a = Assembly.GetEnt ryAssembly();
if (null != a)
s = a.FullName; // this gives me 'ReflectionTest er', but
when called from VB6. a is null in that case
}

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

}
}
}

"Steven Cheng [MSFT]" wrote:
Hi Amdrit,

As for the "GetCallingAsse mbly" 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.MyT ype" is a known type defined in the target assemly I
want
to get reference
=============== =
Type tp =typeof(MyAssem bly.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:
ms****@microsof t.com.

=============== =============== =============== =====
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.
>

--------------------
From: =?Utf-8?B?UVNJRGV2ZWx vcGVy?= <QS**********@n ewsgroup.nospam >
References: <8A************ *************** *******@microso ft.com>
<OI************ **@TK2MSFTNGP05 .phx.gbl>
Subject: Re: Retrieve name of VB6 calling app via reflection from C#
COM in
Date: Mon, 14 Apr 2008 10:43:14 -0700


When I call GetCallingAssem bly 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 MethodImplAttri bute attribute with
MethodImplOptio ns.NoInlining to the C# logging assembly constructor but
it
had no affect.

I also tried GetEntryAssembl y 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:

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?
"QSIDevelop er" <QS**********@n ewsgroup.nospam wrote in message
news:8A******** *************** ***********@mic rosoft.com...
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.GetCal lingAssembly()
but
this fails when I use the VB6 client. Is there a way to get this
information
at runtime?




Jun 27 '08 #7
Thanks for your reply Amdrit,

There does exist a StackTrace class under "System.Diagnos itcs" 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
<appSettingsite ms 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:
ms****@microsof t.com.

=============== =============== =============== =====
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.

--------------------
>Thread-Topic: Retrieve name of VB6 calling app via reflection from C# COM i
From: =?Utf-8?B?UVNJRGV2ZWx vcGVy?= <QS**********@n ewsgroup.nospam >
References: <8A************ *************** *******@microso ft.com>
<OI************ **@TK2MSFTNGP05 .phx.gbl>
<77************ *************** *******@microso ft.com>
<pK************ **@TK2MSFTNGHUB 02.phx.gbl>
<B9************ *************** *******@microso ft.com>
<Rm************ **@TK2MSFTNGHUB 02.phx.gbl>
>Subject: Re: Retrieve name of VB6 calling app via reflection from C# COM in
Date: Wed, 16 Apr 2008 04:18:00 -0700
>
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.co nfig. 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
(GetCallingAsse mbly
>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:
>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 "GetCallingAsse mbly"
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 "GetCallingAsse mbly" will return
"ReflectionTes t" because it is the immediate caller of the LogInfo
method.
>Also, if you call "GetEntryAssemb ly" 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 "CallingAssmebl y" may also
vary from time to time. It is reasonable to call "GetCallingAsse mbly" 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:
ms****@microsof t.com.

============== =============== =============== ======
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.
>--------------------
>Thread-Topic: Retrieve name of VB6 calling app via reflection from C#
COM
>in
>thread-index: Acie74kZBFIskDv WSkqeSsNJrp7vVQ ==
Subject: Re: Retrieve name of VB6 calling app via reflection from C#
COM in
>Date: Tue, 15 Apr 2008 04:55:01 -0700
>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.GetCal lingAssembly(). FullName; //
this
>returns 'Logger'
string s;
Assembly a = Assembly.GetEnt ryAssembly();
if (null != a)
s = a.FullName; // this gives me 'ReflectionTest er',
but
>when called from VB6. a is null in that case
}

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

}
}
}

"Steven Cheng [MSFT]" wrote:

Hi Amdrit,

As for the "GetCallingAsse mbly" 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.My Type" is a known type defined in the target assemly I
want
>to get reference
============== ==
Type tp =typeof(MyAssem bly.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:
ms****@microsof t.com.

============== =============== =============== ======
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.
>>

--------------------
From: =?Utf-8?B?UVNJRGV2ZWx vcGVy?= <QS**********@n ewsgroup.nospam >
References: <8A************ *************** *******@microso ft.com>
<OI*********** ***@TK2MSFTNGP0 5.phx.gbl>
Subject: Re: Retrieve name of VB6 calling app via reflection from C#
COM in
>Date: Mon, 14 Apr 2008 10:43:14 -0700
When I call GetCallingAssem bly 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 MethodImplAttri bute attribute with
MethodImplOpti ons.NoInlining to the C# logging assembly constructor
but
>it
>had no affect.

I also tried GetEntryAssembl y 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:

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?
"QSIDevelope r" <QS**********@n ewsgroup.nospam wrote in message
news:8A******* *************** ************@mi crosoft.com...
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.GetCa llingAssembly()
but
this fails when I use the VB6 client. Is there a way to get
this
information
at runtime?



Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1888
by: Antonio Paglia | last post by:
Imaginate you have one method that accept some arguments like: 1- a control 2- un Type (MyType) 3- el nombre de una Propiedad "PropertyName" Example: .Add(Me.txtCustomerID, GetType(Customer), "CustomerID")
1
3216
by: Jeff Molby | last post by:
Sorry for the crossposting guys. I figured this one might be a little tricky, so I'm calling upon the C# brains out there to help out this poor VB developer. I know enough C# to translate any code you can offer, if necessary. I have a very loosely coupled WinForms app written in VB.Net, Framework v1.1 I need to take a string value which contains the unqualified name of a class, find the assembly that contains such a class, and...
6
7569
by: mphanke | last post by:
Hi, how can I retrieve the icon for a registered file extension from the registry and show it in my ListView? Any hints appreciated, Martin
0
1941
by: karunakar | last post by:
Hi All I am not able to read the class name I want read the particular class name string path = System.Configuration.ConfigurationSettings.AppSettings; string className = path + ".User"; return (SCS.SR.IDAL.IUser) Assembly.Load(path).CreateInstance(className); ( This line iam getting Error)
2
5852
by: Don | last post by:
This may seem like an odd question, but is it possible to get the name of the class & function that is calling a function of another class? e.g. Public Class CallerName Public Shared Function Test() As String Return <the name of my caller> ' <--- is there a way to do this? End Sub End Class
9
6824
by: Don | last post by:
Say I have a class like so: Public Class MyClass Public Prop1 as Integer Public Prop2 As Integer Public Prop3 As Integer End Class Is it possible to retrieve a list of the variables or objects declared within an instance of that class if they are declared with Public (or
3
1341
by: Udi | last post by:
Hi, given a member in a class, I would like to print it's name automatically (via reflection i guess). Say I have an ArrayList of objects that contains references to members of a derived class. How do I retrieve their names? Example: class Base
4
2140
by: ssg31415926 | last post by:
I want to write a logging method. I want it to log the name of the calling class and method. Is there any way to do this? I presume it'll use Reflection but it's not an area I've used much.a Alternatively, is there a piece of code I can use to pass in the name of the method, so that I can copy and paste the calling code and don't have to remember to change a hard coded method name. I could code this:
15
8198
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a Delegate. My testing showed that actually it was six times faster: 0.5 seconds for 100,000 iterations versus 3.1 seconds. Can anyone explain why? Something in the way I coded it? I'd appreciate any insights. Here's the code (in a Windows Form...
0
8617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8553
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8971
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8815
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5827
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.