Farzin Zaker
Farzin Zaker

Reputation: 3606

How to find caller assembly name in unmanaged c++ dll

I have an unmanaged c++ dll. I am calling external methods of this unmanaged dll from c# (.net 3.5)

I am looking for a way to find witch c# assembly is calling my unmanaged c++ dll (into my c++ dll) (at least, name of assembly)

And sure, I don't want to pass any additional parameter to methods.

Thanks in advance

Upvotes: 3

Views: 1840

Answers (3)

Farzin Zaker
Farzin Zaker

Reputation: 3606

Finally I found the solution.

I was looking for a way to restrict not allowed access to my unmanaged dll. So I crawled the stack trace for my caller assembly location.

Finally I decided to check public key token of caller assembly (found this way) and validate it.

Thanks from you time and answers...

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942358

This requires a stack walk. Works well in managed code, that's how code access security is implemented. Does not work so well when there are native stack frames to walk. You can try StackWalk64() in your native code. Expensive and not that likely to work out well, especially in .NET 4.0 where the CLR no longer fakes the module. Be very wary of the frame pointer omission optimization option.

Don't do this, I'd say. It is so much easier to solve simply by letting the managed code pass an extra argument.

Upvotes: 1

Puppy
Puppy

Reputation: 147036

You can't know. Your external methods could be called by any C-compatible language and as such Windows and the CRT store nothing extra about CLR languages.

Upvotes: 0

Related Questions