473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Return value from C++ dll called from C# app

How would I do, in both ends, to get a return value from a C++ dll called
from C# app?
Feb 11 '07 #1
4 2041
More specifically, I would like the return value to be a HRESULT (as it
already is), but I would like to pass a parameter which I can change in the
DLL so that it also has effect in the C# app.

"Joachim" wrote:
How would I do, in both ends, to get a return value from a C++ dll called
from C# app?
Feb 11 '07 #2
>More specifically, I would like the return value to be a HRESULT (as it
already is), but I would like to pass a parameter which I can change in the
DLL so that it also has effect in the C# app.
So something like this?

HRESULT _stdcall MyFunc(int *pi)
{
if (!pi) return E_POINTER;
*pi = 42;
return S_OK;
}

---

[DllImport("your.dll")]
static extern uint MyFunc(ref int pi);

....

int i = 0;
uint hr = MyFunc(ref i);
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 11 '07 #3

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>
>>More specifically, I would like the return value to be a HRESULT (as it
already is), but I would like to pass a parameter which I can change in
the
DLL so that it also has effect in the C# app.

So something like this?

HRESULT _stdcall MyFunc(int *pi)
{
if (!pi) return E_POINTER;
*pi = 42;
return S_OK;
}

---

[DllImport("your.dll")]
static extern uint MyFunc(ref int pi);

Works, but bleech, you need untrusted code permission. If you can compile
the C++ code as a managed assembly (C++/CLI), then you would use

public ref class A
{
HRESULT MyFunc(System::Int32% intbyref) { intbyref = 42; return S_OK; }
}

which has almost the identical caller interface, but no need for DllImport
(use a managed reference instead) and needs no special permissions (since
this code will compile with /clr:safe)
...

int i = 0;
uint hr = MyFunc(ref i);
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 12 '07 #4
"Ben Voigt" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>>
>>>More specifically, I would like the return value to be a HRESULT (as it
already is), but I would like to pass a parameter which I can change in the
DLL so that it also has effect in the C# app.

So something like this?

HRESULT _stdcall MyFunc(int *pi)
{
if (!pi) return E_POINTER;
*pi = 42;
return S_OK;
}

---

[DllImport("your.dll")]
static extern uint MyFunc(ref int pi);


Works, but bleech, you need untrusted code permission. If you can compile the C++ code as
a managed assembly (C++/CLI), then you would use

public ref class A
{
HRESULT MyFunc(System::Int32% intbyref) { intbyref = 42; return S_OK; }
}

which has almost the identical caller interface, but no need for DllImport (use a managed
reference instead) and needs no special permissions (since this code will compile with
/clr:safe)
True, but the OP is talking about existing native C++ code I guess, Mattias is only
illustrating, by example, how you can declare a function that returns an HRESULT and passes
a value by reference.
Note that you can apply the "SuppressUnmanagedCodeSecurityAttribute" to suppress the
security walk when using PInvoke, this achieves the same result as C++/CLI's managed/native
code interop.

Wily.

Feb 13 '07 #5

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

Similar topics

12
by: Ximo | last post by:
Can I do a function which don't return anything? The question is that, if I do a function that have a return or without return, it returns always "None", but i want that it doesnt return me...
3
by: bipod.rafique | last post by:
Hello all, Even though this topic has been discussed many times, I still need your help in clearing my confusions. Consider the following code: class aclass{ public: aclass(){
1
by: Capstar | last post by:
Hi NG, I have the following struct, macro and functions prototypes: typedef struct { jmp_buf __env; /*other stuff*/ } SAT; #define smalloc_init(sat_p, size) \
5
by: siaj | last post by:
Hello, I have a javascript function for a validation in the HTML page of the asp.Net page.. I call this function in a Savebutton click When the validation fails No postback should happen ( ie...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: info | last post by:
Hi... I want to know (inside a function) if the calling code wants a return value or not. Here is an example: function foo() {
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
3
by: MathWizard | last post by:
Hi, I have a question about returning an object in a function and calling a copy constructor. As far as I understand, in the following code the copy constructor may or may not be called,...
4
by: banderson | last post by:
Hello amazing vba writers, I am trying to make a combo box return a value based on a combo box selection. I have tried a number of the codes posted here and am still having problems. I think it is...
1
by: Nikhil.S.Ketkar | last post by:
Hi, Does the following construct qualify as overloading on return type ? If not, in what way ? I overloaded the type conversion operator and used pointers to member functions. Its pretty...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.