By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Best way for C# application to access VC application

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Ashutosh Bhawasinka" wrote:
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
Jun 27 '08 #2

P: n/a
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.
Jun 27 '08 #3

P: n/a
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.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Ashutosh Bhawasinka" wrote:
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.
Jun 27 '08 #4

P: n/a
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:
ms****@microsoft.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.

--------------------
>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.
Jun 27 '08 #5

P: n/a
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:
ms****@microsoft.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: st*****@online.microsoft.com (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:
ms****@microsoft.com.

================================================= =
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ault.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.

Jun 27 '08 #6

P: n/a
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

Steven Cheng [MSFT] wrote:
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:
ms****@microsoft.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: st*****@online.microsoft.com (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:
ms****@microsoft.com.

================================================= =
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ault.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.
Jun 27 '08 #7

P: n/a
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
--------------------
>Date: Sat, 17 May 2008 22:05:17 +0530
From: Ashutosh Bhawasinka <sm*******@nospam.nospam>
User-Agent: Thunderbird 2.0.0.14 (Windows/20080421)
MIME-Version: 1.0
Subject: Re: Best way for C# application to access VC application
rp
>
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

Steven Cheng [MSFT] wrote:
>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

Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.