Best way for C# application to access VC application

  • Thread starter Ashutosh Bhawasinka
  • Start date
A

Ashutosh Bhawasinka

Hi,
I have a C# .Net application which needs to use some feature which can
be only developed in Visual C++ (its extended MAPI).

The C# exe will be supplied to users without a setup.

What kind of exe/dll should I develop in Visual C++ so that I can meet
these requirements

1) No administrator rights are required
2) C# application can use the feature in the VC dll/exe if the dll is
present in the same folder and should run even if its not there.
3) If required the VC exe/dll can be embedded inside the C# exe as a
resource and when required the file can created by extracting the
resource by application itself.

As per my understanding, dynamic link library is the only solution. If
there is any other option please let me know.

Regards,
Ashutosh Bhawasinka
 
P

Peter Ritchie [C# MVP]

You can PInvoke any exported functions in any binary, EXE or DLL.

If it's a native-only C++ binary then you can only PInvoke exported
functions. i.e. you can't get access to native classes from a managed
language.

If you want to access native classes from a managed language you can write
managed wrappers for them in C++ and produce a mixed-mode DLL. This allows
other managed applications to access the managed classes in this mixed-mode
DLL that in term access the native classes.

This shouldn't require administrative rights (other than for install, which
you've said isn't an issue) but the mixed-mode DLL will likely need unsafe
code rights in CAS.
 
A

Ashutosh Bhawasinka

Thanks for your comments. I can't create a mixed mode dll as you said
because the user won't even have the admin rights for installation.

So, I have decided to create a native C++ dll.

Now I have two choices, either to use PInvoke or use reflection to
access the functions.

Which one do you suggest?? The application should run even if the DLL is
not present. As per my understanding reflection is OK for this, but I
haven't tried PInvoke with the case when the dll is not present.

Regards,
Ashutosh Bhawasinka
 
P

Peter Ritchie [C# MVP]

You can't use refelection on a native binary, so Pinvoke is the only option.
As ons as you don't try to execute the PInvoke methods when the DLL isn't
there, you should have no problems.
 
S

Steven Cheng [MSFT]

Hi Ashutosh,

I agree with Peter that for accessing native c/c++ dll, you cannot use .NET
reflection which is used for managed assembly only. So far calling native
dll via PInvoke is the reasonable approach for this scenario.

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:
(e-mail address removed).

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

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

--------------------
 
S

Steven Cheng [MSFT]

Hi Ashutosh,

Have you got progress on this issue? Did you use the PINVOKE approach for
calling the NATIVE code? If there is any further questions, welcome to post
here.

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:
(e-mail address removed).

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

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

--------------------
From: (e-mail address removed) (Steven Cheng [MSFT])
Organization: Microsoft
Date: Thu, 01 May 2008 04:10:51 GMT
Subject: Re: Best way for C# application to access VC application
 
A

Ashutosh Bhawasinka

Hi Steve,

Need your help again. I am having a problem.

I have a C++ DLL, it exports some some plain C style functions. This
functions internally initializes COM and then MAPI. This works fine if I
use the DLL from a C++ application (I tested with a C++ Win32 console
application).

But, when I call/invoke those functions from a C# .Net application using
PInvoke, COM initialization is fails in the DLL.

Do I need to initialize something??

Regards,
Ashutosh
Hi Ashutosh,

Have you got progress on this issue? Did you use the PINVOKE approach for
calling the NATIVE code? If there is any further questions, welcome to post
here.

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:
(e-mail address removed).

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

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

--------------------
From: (e-mail address removed) (Steven Cheng [MSFT])
Organization: Microsoft
Date: Thu, 01 May 2008 04:10:51 GMT
Subject: Re: Best way for C# application to access VC application
Hi Ashutosh,

I agree with Peter that for accessing native c/c++ dll, you cannot use .NET
reflection which is used for managed assembly only. So far calling native
dll via PInvoke is the reasonable approach for this scenario.

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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#noti f
ications.

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

--------------------
Date: Thu, 01 May 2008 00:12:45 +0530
MIME-Version: 1.0
Subject: Re: Best way for C# application to access VC application
Thanks for your comments. I can't create a mixed mode dll as you said
because the user won't even have the admin rights for installation.

So, I have decided to create a native C++ dll.

Now I have two choices, either to use PInvoke or use reflection to
access the functions.

Which one do you suggest?? The application should run even if the DLL is
not present. As per my understanding reflection is OK for this, but I
haven't tried PInvoke with the case when the dll is not present.

Regards,
Ashutosh Bhawasinka

Peter Ritchie [C# MVP] wrote:
You can PInvoke any exported functions in any binary, EXE or DLL.

If it's a native-only C++ binary then you can only PInvoke exported
functions. i.e. you can't get access to native classes from a managed
language.

If you want to access native classes from a managed language you can write
managed wrappers for them in C++ and produce a mixed-mode DLL. This allows
other managed applications to access the managed classes in this mixed-mode
DLL that in term access the native classes.

This shouldn't require administrative rights (other than for install, which
you've said isn't an issue) but the mixed-mode DLL will likely need unsafe
code rights in CAS.
 
S

Steven Cheng [MSFT]

Hi Ashutosh,

Thanks for your followup. I've also seen your new posts regarding on the
COM interop issue. Some other engineers have been working with you there.
Welcome to continue discuss in those threads.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
--------------------
 

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