Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
hI,

I have 2 DLL's loaded at runtime. But I want to add a log in the function calls to know the origin of the function call. (i.e) Function A can be called from DLL A all the times and Function B can be called from DLL B all the times. Is there any function to print the DLL name of the particular fucntion called.
Posted
Comments
Manfred Rudolf Bihy 13-Jan-11 15:08pm    
Not that I have an answer, but I really like your question. 5+
I hope somebody comes up with a satisfactory answer. Good luck!
Sergey Alexandrovich Kryukov 13-Jan-11 15:27pm    
The question is good; I put my 5 immediately.
Manfred Rudolf Bihy 13-Jan-11 15:37pm    
Please refer to SAKryukovs answer below mine for seeing how to get the assembly and by that you can get the name. It would also do you good if you studied the StackTrace class example on MSDN as I pointed out in my answer.
Happy coding!
Manfred Rudolf Bihy 13-Jan-11 20:12pm    
Moved to comment for OP:
I just found the exact answer from the below link:
"http://www.codeproject.com/KB/DLL/DLLModuleFileName.aspx"

I googled some and found something in the dot net framework. The example I found is in VB.NET but since you're using C++/CLI (managed C++) it should translate easily especially when you consult the MSDN documentation.

Here it goes. The example uses the System.Reflection namespace to inspect the stacktrace:
VB
Dim method As System.Reflection.MethodBase = New StackTrace().GetFrame(1).GetMethod()
MessageBox.Show(method.Name)


So please visit here to read more about it: http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx[^]

Hope this helps! Please tell us if you got that to work for you.

Best Regards,
Manfred
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jan-11 15:25pm    
Manfred, my 5, but I think fully qualified name is better, so one may need Assembly as well.
I answered, started first by you type faster.
Sergey Alexandrovich Kryukov 13-Jan-11 15:27pm    
Oh, this is because you forget to use C++
(how about 4, or you want to fix it? :-)
Manfred Rudolf Bihy 13-Jan-11 15:33pm    
@Sakryukov: My answer is really sufficient. Force feeding OP the solution is not my intend but rather pointing him to the right class to use (System.Reflection.StacktTace). If he reads the document I pointed him to he'll understand how to use it or when OP has a problem with it leave a comment.
I also told you in my comment to your answer that it is more complete than mine. I still think helping people to help themselves is more productive in the end. :)

Thanks for your comment anyway!
Sergey Alexandrovich Kryukov 13-Jan-11 15:33pm    
Manfred, I'm also glad the question was asked.
To be even more intrigued, look at my article "Wish You Were Here... Only Once" -- way more advanced, so much that attracted both lamers and decent developers.
Sangeetha682002 13-Jan-11 15:33pm    
Thanks a lot for the valuable info. But, how can i get the DLL name???
Yes, as far as DLLs are managed assemblies. Here is how:

C++
System::Diagnostics::StackTrace^ stackTrace = gcnew System::Diagnostics::StackTrace();
int count = stackTrace->FrameCount;
for (int level = 0; level < count; level++) {
    System::Diagnostics::StackFrame^ frame =    
          stackTrace->GetFrame(level);
    System::Reflection::MethodBase^ method = frame->GetMethod();
    System::Type^ type = method->GetType();
    System::Reflection::Assembly^ assembly = type->Assembly;
    System::String^ location = assembly->Location;
    System::String^ name = method->Name;
    //use name and location
} //loop frames


Sorry if you find it a bit "sharpish" -- I just translated from my C# code and added a bit.

For more incite, see my article:
Wish You Were Here… Only Once[^]

[EDIT]

Wait! I just realized, maybe we all answered not exactly your question.
It gives some ideas, but it may be not that.

If I'm right, the final answers will depend on many other factors. Please indicate:

1) Do you need information about managed assembly calls or also native unmanaged?
2) Can you modify those DLLs?
3) At what time do you want to obtain the names: if at the time of loading library/assembly -- this is trivial, if on each call -- may or may not be possible, depending on other factors.
4) How exactly do you load DLLs?
5) Do you need just first-level calls (from your project) or also all indirect calls?
5a) Do you want only DLLs directly loaded from your project or also indirectly loaded?

Thank you.
--SA
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 13-Jan-11 15:26pm    
I'm glad OP asked that question. This StackTrace thing is intriguing to say the least.
Thanks for coming up with a better example than mine.
Take 5!
Since you are using c++/cli you can use Cecil[^] to inject logging functionality.

Here are two examples using cecil to inject functionality:
http://stackoverflow.com/questions/4261645/inject-property-call-with-mono-cecil[^]

http://sourceforge.net/projects/reflexil/[^]

The last one is fairly powerful.

Hopefully this is enough to get you started.

Regards
Espen Harlinn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900