CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2012
    Posts
    7

    Passing variables from C++ DLL to C# program

    Hello!

    I have written a C++ DLL for a friend, and his program is coded in C#. I currently make use of the Dllimport function to reference functions in the program, but I have seemed to hit a bit of a wall.

    My final goal is to be able to change the value of a variable in the DLL through the C# program.

    IE: If I have a boolean in my DLL, that is somehow made external, the C# program would be able to set it to true.
    I know I could just pass variables through an "empty function" that takes input parameters and sets them to variables in the DLL, but I am looking for a bit of a better solution.

    I am sure there is a Windows veteran out there who can help me! I would more than appreciate the help.


    -Kaleb

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Passing variables from C++ DLL to C# program

    Quote Originally Posted by KalebTheProgrammer View Post
    My final goal is to be able to change the value of a variable in the DLL through the C# program.
    Provide a function to set the variable. Nothing could be anymore straightforward.
    IE: If I have a boolean in my DLL,
    If you mean the C++ bool type, it is just that -- a C++ type. You cannot assume you can directly set this type correctly from another language.

    The only types guaranteed to be cross-language are the Windows types such as LONG, BOOL, DWORD, TCHAR, etc. and pointers to these types.
    I know I could just pass variables through an "empty function" that takes input parameters and sets them to variables in the DLL, but I am looking for a bit of a better solution.
    Quite the contrary -- setting the variable directly is the inferior method.

    What if you decide to rename this variable? Does the C# code also have to change just because you changed the name of the variable? What if you change the type, say a "bool" is no longer adequate, or maybe it's more complex than just setting a boolean? Does the C# code have to know all the internals and then have to make coding changes also?

    If you create a function, you can change that variable's name, its types, or whatever else internal to something else, and the C# code won't have to change since the function name won't change.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jun 2012
    Posts
    7

    Re: Passing variables from C++ DLL to C# program

    Thanks Paul!

    Sorry for the variable confusion there in the beginning!
    I have never really done this sort of thing before, so I was unaware that I had my options reversed. Now that I think about it, passing variables into a function really does seem like the better option. I had assumed for whatever reason that creating a function just to pass variables seemed to be a bit excessive, silly me. I appreciate you setting me on the right path!

    -Kaleb

  4. #4
    Join Date
    Jun 2012
    Posts
    7

    Re: Passing variables from C++ DLL to C# program

    To add on to my original question, how would I pass variables FROM the DLL to the C# program?

    -Kaleb

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Passing variables from C++ DLL to C# program

    Quote Originally Posted by KalebTheProgrammer View Post
    To add on to my original question, how would I pass variables FROM the DLL to the C# program?

    -Kaleb
    You shouldn't. You provide functions to set the variable and query the state of that variable. But again, today it's a simple variable, next week you realize that to accomplish the same task, you need to set that variable and do other housekeeping.

    If you want a real life example, look at the Windows API function SetLastError and GetLastError. Why doesn't Windows just have a DWORD variable that your program can set or get? It's for the same reasons as I stated previously.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Passing variables from C++ DLL to C# program

    Quote Originally Posted by KalebTheProgrammer View Post
    To add on to my original question, how would I pass variables FROM the DLL to the C# program?
    If you would want the C# program to directly use variables from the C++ DLL then your best bet would be to use C++/CLI instead. However, that seems overkill if you're talking about a single boolean variable.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Jun 2012
    Posts
    7

    Re: Passing variables from C++ DLL to C# program

    Thanks for all the help guys, I appreciate it.
    It appears that the way I phrased my question seems to be causing some miscommunication.

    The C# program needs to read/modify 20 variables inside the DLL (This can be accomplished by passing them through a function like some of you have suggested) but the DLL needs to also be capable of passing 20 variables back to the C# program. The second part of that statement is where I am currently stuck.

    Perhaps that will help clear up any confusion my original question created!

    -Kaleb

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Passing variables from C++ DLL to C# program

    Quote Originally Posted by KalebTheProgrammer View Post
    [...] but the DLL needs to also be capable of passing 20 variables back to the C# program. The second part of that statement is where I am currently stuck.
    If that implies an active query (i.e. polling) initiated from the side of the C# program, you can use functions on the C++ side that merely return the value of the respective variable (getters), much like you use functions to set the values (setters). OTOH, if you want some means for the C++ DLL to (quasi-)actively manipulate variables on the C# side, you'd probably need events or callbacks, and in such a scenario C++/CLI may actually be the adequate tool.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Passing variables from C++ DLL to C# program

    Quote Originally Posted by KalebTheProgrammer View Post
    Thanks for all the help guys, I appreciate it.
    It appears that the way I phrased my question seems to be causing some miscommunication.

    The C# program needs to read/modify 20 variables inside the DLL
    The real question is why the client (C# program) needs to know all of these details, i.e. 20 variables. Shouldn't all of these details be hidden from the client, and just allow the client to call high-level functions that internally manipulate these variables? That's how a well-designed system is supposed to work. Today it's 20 variables, tomorrow it's 25 variables, then you come up with a better idea and the next day it's 7 variables. Is the C# client going to change their code 3 times?

    For example, let's say I have a class that dynamically resizes an array. Do I want the person using this class to know exactly what variables are used to add an element to the array? Of course not -- there would be a function called "Add" or something similar to that. Why should the client care if 1, 2, or 20 variables are involved in performing this function, as long as it does its job.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Jun 2012
    Posts
    7

    Re: Passing variables from C++ DLL to C# program

    Paul, I see what you are getting at but I'm doing terrible at explaining.

    So the C# Program is a game my friend wrote. The game has 4 characters, with X,Y and rotation in degrees as variables for each.
    The C# game needs to pass it's character's data (X,Y,D) into the C++ DLL so that it can be transmitted. The DLL however also RECEIVES coordinates from other clients. That data needs to be passed from the DLL to the C# program so that the game can update the character's positions. And I know it doesn't sound time-friendly, but I have the game source code if I need to add more variables.

    You have some good suggestions Paul, but humor me for a second. Is there any way you know of to pass the variables as I have detailed? I don't mind if it's inefficient or a bad idea.

    -Kaleb

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Passing variables from C++ DLL to C# program

    Quote Originally Posted by KalebTheProgrammer View Post
    So the C# Program is a game my friend wrote. The game has 4 characters, with X,Y and rotation in degrees as variables for each.
    The C# game needs to pass it's character's data (X,Y,D) into the C++ DLL so that it can be transmitted. The DLL however also RECEIVES coordinates from other clients. That data needs to be passed from the DLL to the C# program so that the game can update the character's positions. And I know it doesn't sound time-friendly, but I have the game source code if I need to add more variables.
    So, you can use two exported functions.
    Code:
    void SetCharacterInfo(int id, int x, int y, int d);
    void GetCharacterInfo(int id, int* x, int* y, int* d);
    What's wrong with this?
    If you want, you can add a return code for error handling or use a struct to make the character info easier to extend. But in essence you only need these two functions to do what you've described.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Passing variables from C++ DLL to C# program

    Quote Originally Posted by KalebTheProgrammer View Post
    Paul, I see what you are getting at but I'm doing terrible at explaining.

    So the C# Program is a game my friend wrote. The game has 4 characters, with X,Y and rotation in degrees as variables for each.
    The C# game needs to pass it's character's data (X,Y,D) into the C++ DLL so that it can be transmitted. The DLL however also RECEIVES coordinates from other clients. That data needs to be passed from the DLL to the C# program so that the game can update the character's positions. And I know it doesn't sound time-friendly, but I have the game source code if I need to add more variables.
    D_Drmmr basically sums up one way to do this. All you needed was one function to get the data for a specific character and one function to retrieve the data for a specific character.

    You don't even need to bring up C# at all or even DLL's -- how would you have developed a C++ program that needed to set/get information contained from function to function? Wouldn't you have some central, private, data structure, and if you wanted to set/get a certain values in that structure, you would call public functions specifying which element in the structure and what the values should be? That winds up in what D_Drmmr described.

    You have some good suggestions Paul, but humor me for a second.
    In general, if you find yourself having to change your program each and every time something relatively simple needs to change, then you have to look again at your design. Imagine if you had to change the source to the Windows OS every time you write a different Windows program because your program uses 20 windows instead of some set number. I know that's an extreme example, but it is in essence what you were thinking of doing for your program.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured