how to create conditions in a dll that can be changed through the application running it





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















My question is simple, I have a dll that I wrote in c++ and I'm calling it from a c# program. My question is if I can have a variable in the c# program that can be used in the dll that is written in C++ and change conditions inside the program when changing the variable. ex:





C#:



int z = 0;  //variable which i want to know if it can be used inside the dll.
[DllImport("C:\Users\examplelibtest.dll")] //path to the dll
public static extern void libtest(int x); //




C++:



#include "C:\Users\app.cpp"   //path to the actual program
#include "C:\Users\Header.h" //^^

extern "C" __declspec(dllexport) void libtest(int x) {

myClass libtestx(x);
}




C++ (Header.h):



#pragma once

class myClass
{
public:
myClass(int x);
private:
//bool z;
};




C++ (app.cpp):



void dosomething():
//some method to do something
while(z == 0){ //heres my question, can a var (z) inside the c# program set the conditions?
//do something
}
myClass::myClass(int x) {
if (x == 1) {
dosomething();
}
else if (x == 0) {
//do nothing
}

}




note that I just started learning these two languages and everything I have been learning and writing I learned by just asking questions and reading online so sorry If I sound stupid or my code is trash. I'll be very thankful for any help regarding my question or any tips that could help me improve. Thanks again.










share|improve this question

























  • Despite the frankly laughable premise behind this question, I don't think it deserves to be downvoted/closed. Its both well written and answerable.

    – BradleyDotNET
    Nov 16 '18 at 20:53


















0















My question is simple, I have a dll that I wrote in c++ and I'm calling it from a c# program. My question is if I can have a variable in the c# program that can be used in the dll that is written in C++ and change conditions inside the program when changing the variable. ex:





C#:



int z = 0;  //variable which i want to know if it can be used inside the dll.
[DllImport("C:\Users\examplelibtest.dll")] //path to the dll
public static extern void libtest(int x); //




C++:



#include "C:\Users\app.cpp"   //path to the actual program
#include "C:\Users\Header.h" //^^

extern "C" __declspec(dllexport) void libtest(int x) {

myClass libtestx(x);
}




C++ (Header.h):



#pragma once

class myClass
{
public:
myClass(int x);
private:
//bool z;
};




C++ (app.cpp):



void dosomething():
//some method to do something
while(z == 0){ //heres my question, can a var (z) inside the c# program set the conditions?
//do something
}
myClass::myClass(int x) {
if (x == 1) {
dosomething();
}
else if (x == 0) {
//do nothing
}

}




note that I just started learning these two languages and everything I have been learning and writing I learned by just asking questions and reading online so sorry If I sound stupid or my code is trash. I'll be very thankful for any help regarding my question or any tips that could help me improve. Thanks again.










share|improve this question

























  • Despite the frankly laughable premise behind this question, I don't think it deserves to be downvoted/closed. Its both well written and answerable.

    – BradleyDotNET
    Nov 16 '18 at 20:53














0












0








0








My question is simple, I have a dll that I wrote in c++ and I'm calling it from a c# program. My question is if I can have a variable in the c# program that can be used in the dll that is written in C++ and change conditions inside the program when changing the variable. ex:





C#:



int z = 0;  //variable which i want to know if it can be used inside the dll.
[DllImport("C:\Users\examplelibtest.dll")] //path to the dll
public static extern void libtest(int x); //




C++:



#include "C:\Users\app.cpp"   //path to the actual program
#include "C:\Users\Header.h" //^^

extern "C" __declspec(dllexport) void libtest(int x) {

myClass libtestx(x);
}




C++ (Header.h):



#pragma once

class myClass
{
public:
myClass(int x);
private:
//bool z;
};




C++ (app.cpp):



void dosomething():
//some method to do something
while(z == 0){ //heres my question, can a var (z) inside the c# program set the conditions?
//do something
}
myClass::myClass(int x) {
if (x == 1) {
dosomething();
}
else if (x == 0) {
//do nothing
}

}




note that I just started learning these two languages and everything I have been learning and writing I learned by just asking questions and reading online so sorry If I sound stupid or my code is trash. I'll be very thankful for any help regarding my question or any tips that could help me improve. Thanks again.










share|improve this question
















My question is simple, I have a dll that I wrote in c++ and I'm calling it from a c# program. My question is if I can have a variable in the c# program that can be used in the dll that is written in C++ and change conditions inside the program when changing the variable. ex:





C#:



int z = 0;  //variable which i want to know if it can be used inside the dll.
[DllImport("C:\Users\examplelibtest.dll")] //path to the dll
public static extern void libtest(int x); //




C++:



#include "C:\Users\app.cpp"   //path to the actual program
#include "C:\Users\Header.h" //^^

extern "C" __declspec(dllexport) void libtest(int x) {

myClass libtestx(x);
}




C++ (Header.h):



#pragma once

class myClass
{
public:
myClass(int x);
private:
//bool z;
};




C++ (app.cpp):



void dosomething():
//some method to do something
while(z == 0){ //heres my question, can a var (z) inside the c# program set the conditions?
//do something
}
myClass::myClass(int x) {
if (x == 1) {
dosomething();
}
else if (x == 0) {
//do nothing
}

}




note that I just started learning these two languages and everything I have been learning and writing I learned by just asking questions and reading online so sorry If I sound stupid or my code is trash. I'll be very thankful for any help regarding my question or any tips that could help me improve. Thanks again.







c# c++ dll






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 20:54









πάντα ῥεῖ

74.1k1077144




74.1k1077144










asked Nov 16 '18 at 20:44









Sam RafieiSam Rafiei

287




287













  • Despite the frankly laughable premise behind this question, I don't think it deserves to be downvoted/closed. Its both well written and answerable.

    – BradleyDotNET
    Nov 16 '18 at 20:53



















  • Despite the frankly laughable premise behind this question, I don't think it deserves to be downvoted/closed. Its both well written and answerable.

    – BradleyDotNET
    Nov 16 '18 at 20:53

















Despite the frankly laughable premise behind this question, I don't think it deserves to be downvoted/closed. Its both well written and answerable.

– BradleyDotNET
Nov 16 '18 at 20:53





Despite the frankly laughable premise behind this question, I don't think it deserves to be downvoted/closed. Its both well written and answerable.

– BradleyDotNET
Nov 16 '18 at 20:53












1 Answer
1






active

oldest

votes


















1














No (to your approach)



Lets ignore the difference in languages for the moment; the whole point of libraries is to be reusable across different consumers (programs). Thus the library knowing some detail of the application using it (like a variable) totally defeats the purpose.



Moreover, it creates a circular dependency, no compiler is going to go for that.



Yes (applications can mutate library object state)



Programs do this all the time but they do it by creating the appropriate objects from the library and invoking methods on them (or mutating state, etc.). Say myClass had a method setZ that set its local z variable and that was called from the running application (it could even use the value of its (the programs) z variable!) Then subsequent calls to myClass would use that updated value.



On the whole, it seems like a revisit to OOP fundamentals and what libraries are for may be in order.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345126%2fhow-to-create-conditions-in-a-dll-that-can-be-changed-through-the-application-ru%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    No (to your approach)



    Lets ignore the difference in languages for the moment; the whole point of libraries is to be reusable across different consumers (programs). Thus the library knowing some detail of the application using it (like a variable) totally defeats the purpose.



    Moreover, it creates a circular dependency, no compiler is going to go for that.



    Yes (applications can mutate library object state)



    Programs do this all the time but they do it by creating the appropriate objects from the library and invoking methods on them (or mutating state, etc.). Say myClass had a method setZ that set its local z variable and that was called from the running application (it could even use the value of its (the programs) z variable!) Then subsequent calls to myClass would use that updated value.



    On the whole, it seems like a revisit to OOP fundamentals and what libraries are for may be in order.






    share|improve this answer




























      1














      No (to your approach)



      Lets ignore the difference in languages for the moment; the whole point of libraries is to be reusable across different consumers (programs). Thus the library knowing some detail of the application using it (like a variable) totally defeats the purpose.



      Moreover, it creates a circular dependency, no compiler is going to go for that.



      Yes (applications can mutate library object state)



      Programs do this all the time but they do it by creating the appropriate objects from the library and invoking methods on them (or mutating state, etc.). Say myClass had a method setZ that set its local z variable and that was called from the running application (it could even use the value of its (the programs) z variable!) Then subsequent calls to myClass would use that updated value.



      On the whole, it seems like a revisit to OOP fundamentals and what libraries are for may be in order.






      share|improve this answer


























        1












        1








        1







        No (to your approach)



        Lets ignore the difference in languages for the moment; the whole point of libraries is to be reusable across different consumers (programs). Thus the library knowing some detail of the application using it (like a variable) totally defeats the purpose.



        Moreover, it creates a circular dependency, no compiler is going to go for that.



        Yes (applications can mutate library object state)



        Programs do this all the time but they do it by creating the appropriate objects from the library and invoking methods on them (or mutating state, etc.). Say myClass had a method setZ that set its local z variable and that was called from the running application (it could even use the value of its (the programs) z variable!) Then subsequent calls to myClass would use that updated value.



        On the whole, it seems like a revisit to OOP fundamentals and what libraries are for may be in order.






        share|improve this answer













        No (to your approach)



        Lets ignore the difference in languages for the moment; the whole point of libraries is to be reusable across different consumers (programs). Thus the library knowing some detail of the application using it (like a variable) totally defeats the purpose.



        Moreover, it creates a circular dependency, no compiler is going to go for that.



        Yes (applications can mutate library object state)



        Programs do this all the time but they do it by creating the appropriate objects from the library and invoking methods on them (or mutating state, etc.). Say myClass had a method setZ that set its local z variable and that was called from the running application (it could even use the value of its (the programs) z variable!) Then subsequent calls to myClass would use that updated value.



        On the whole, it seems like a revisit to OOP fundamentals and what libraries are for may be in order.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 20:59









        BradleyDotNETBradleyDotNET

        52.4k87295




        52.4k87295
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345126%2fhow-to-create-conditions-in-a-dll-that-can-be-changed-through-the-application-ru%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine