Is there a specific way to deal with const pointer members of a struct in JNA?Why isn't sizeof for a struct equal to the sum of sizeof of each member?Passing structure through Pointer param in JNA(Found solution by myself)JNA : Pointer to array of Struct : Invalid PointerJNA: how to deal with unkown structs?Getting and passing structs by value in Clojure with JNAJNA callback function with pointer to structure argumentJNA: invalid memory access with callback function parameter (struct)Map a c union into a structure on javaJNA - AMD Overdrive5 API returns null pointer errorJNA How to populate a pointer-to-structure field within a structure to be PASSED to the native library?

If you know the location of an invisible creature, can you attack it?

Can the IPA represent all languages' tones?

Why is there a large performance impact when looping over an array over 240 elements?

Markov-chain sentence generator in Python

Do Reform Jews believe in a theistic God?

Symbol: Put a smile symbol under a plus

Does EU compensation apply to flights where the departure airport closes check-in counters during protests?

How can I communicate my issues with a potential date's pushy behavior?

What is the difference between 王 and 皇?

How much can I judge a company based on a phone screening?

How would you translate this? バタコチーズライス

How to Check all AD userers for "blank" password?

How to find directories containing only specific files

Does fossil fuels use since 1990 account for half of all the fossil fuels used in history?

Modeling the uncertainty of the input parameters

How would timezones work on a planet 100 times the size of our Earth

Are those flyers about apartment purchase a scam?

Can renaming a method preserve encapsulation?

How is являться different from есть and быть

Are there really no countries that protect Freedom of Speech as the United States does?

Swap on SSD in 2019?

Telephone number in spoken words

Word for an event that will likely never happen again

If "more guns less crime", how do gun advocates explain that the EU has less crime than the US?



Is there a specific way to deal with const pointer members of a struct in JNA?


Why isn't sizeof for a struct equal to the sum of sizeof of each member?Passing structure through Pointer param in JNA(Found solution by myself)JNA : Pointer to array of Struct : Invalid PointerJNA: how to deal with unkown structs?Getting and passing structs by value in Clojure with JNAJNA callback function with pointer to structure argumentJNA: invalid memory access with callback function parameter (struct)Map a c union into a structure on javaJNA - AMD Overdrive5 API returns null pointer errorJNA How to populate a pointer-to-structure field within a structure to be PASSED to the native library?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have the following C structure:



typedef struct 
void *instance;
const info_st *info;
core_st;


Which I map to the following Java Class using JNA:



public class core_st extends Structure 

public Pointer instance;
public info_st.ByReference info;

@Override
protected List<String> getFieldOrder()
return Arrays.asList("instance", "info");




I also have the following function taken from a dll:



uint32_t open_core(uint32_t core_id, core_st **core);


And the relative JNA mapping:



int open_core(int core_id, core_st[] core);


Finally, I wrote a java program that calls the function this way:



core_st[] cores = new core_st[1];
MyLibrary.INSTANCE.open_core(0, cores);


The function should populate cores[0] members with the result of the "open" operation. In particular, the two fields are two pointers to something else. What happens is that the void *instance field is always correctly populated, but the info field is always null (a pointer to zero).
If I set the jna.memory_dump option to true, any call of core_st.toString() returns always the same result:



memory dump
[70cb64e7]
[fd7f0000]
[00000000]
[00000000]


It looks like the pointer to the info structure is not in the memory read by the JNA. The same call, performed by a similar C program, works fine, both pointers are correctly populated.
I also tried to change the core_st mapping, just for test purposes:



public class core_st extends Structure 

public long instance;
public long info;

@Override
protected List<String> getFieldOrder()
return Arrays.asList("instance", "info");




But I got no differences in the result. instance gets a non-null value, and info is always null. I am working with a 64bit VM.
I was wondering if the problem could be the const modifier of the info field? Can the const modifier in a struct field of type pointer change the way the struct is stored in memory?










share|improve this question
































    1















    I have the following C structure:



    typedef struct 
    void *instance;
    const info_st *info;
    core_st;


    Which I map to the following Java Class using JNA:



    public class core_st extends Structure 

    public Pointer instance;
    public info_st.ByReference info;

    @Override
    protected List<String> getFieldOrder()
    return Arrays.asList("instance", "info");




    I also have the following function taken from a dll:



    uint32_t open_core(uint32_t core_id, core_st **core);


    And the relative JNA mapping:



    int open_core(int core_id, core_st[] core);


    Finally, I wrote a java program that calls the function this way:



    core_st[] cores = new core_st[1];
    MyLibrary.INSTANCE.open_core(0, cores);


    The function should populate cores[0] members with the result of the "open" operation. In particular, the two fields are two pointers to something else. What happens is that the void *instance field is always correctly populated, but the info field is always null (a pointer to zero).
    If I set the jna.memory_dump option to true, any call of core_st.toString() returns always the same result:



    memory dump
    [70cb64e7]
    [fd7f0000]
    [00000000]
    [00000000]


    It looks like the pointer to the info structure is not in the memory read by the JNA. The same call, performed by a similar C program, works fine, both pointers are correctly populated.
    I also tried to change the core_st mapping, just for test purposes:



    public class core_st extends Structure 

    public long instance;
    public long info;

    @Override
    protected List<String> getFieldOrder()
    return Arrays.asList("instance", "info");




    But I got no differences in the result. instance gets a non-null value, and info is always null. I am working with a 64bit VM.
    I was wondering if the problem could be the const modifier of the info field? Can the const modifier in a struct field of type pointer change the way the struct is stored in memory?










    share|improve this question




























      1












      1








      1








      I have the following C structure:



      typedef struct 
      void *instance;
      const info_st *info;
      core_st;


      Which I map to the following Java Class using JNA:



      public class core_st extends Structure 

      public Pointer instance;
      public info_st.ByReference info;

      @Override
      protected List<String> getFieldOrder()
      return Arrays.asList("instance", "info");




      I also have the following function taken from a dll:



      uint32_t open_core(uint32_t core_id, core_st **core);


      And the relative JNA mapping:



      int open_core(int core_id, core_st[] core);


      Finally, I wrote a java program that calls the function this way:



      core_st[] cores = new core_st[1];
      MyLibrary.INSTANCE.open_core(0, cores);


      The function should populate cores[0] members with the result of the "open" operation. In particular, the two fields are two pointers to something else. What happens is that the void *instance field is always correctly populated, but the info field is always null (a pointer to zero).
      If I set the jna.memory_dump option to true, any call of core_st.toString() returns always the same result:



      memory dump
      [70cb64e7]
      [fd7f0000]
      [00000000]
      [00000000]


      It looks like the pointer to the info structure is not in the memory read by the JNA. The same call, performed by a similar C program, works fine, both pointers are correctly populated.
      I also tried to change the core_st mapping, just for test purposes:



      public class core_st extends Structure 

      public long instance;
      public long info;

      @Override
      protected List<String> getFieldOrder()
      return Arrays.asList("instance", "info");




      But I got no differences in the result. instance gets a non-null value, and info is always null. I am working with a 64bit VM.
      I was wondering if the problem could be the const modifier of the info field? Can the const modifier in a struct field of type pointer change the way the struct is stored in memory?










      share|improve this question
















      I have the following C structure:



      typedef struct 
      void *instance;
      const info_st *info;
      core_st;


      Which I map to the following Java Class using JNA:



      public class core_st extends Structure 

      public Pointer instance;
      public info_st.ByReference info;

      @Override
      protected List<String> getFieldOrder()
      return Arrays.asList("instance", "info");




      I also have the following function taken from a dll:



      uint32_t open_core(uint32_t core_id, core_st **core);


      And the relative JNA mapping:



      int open_core(int core_id, core_st[] core);


      Finally, I wrote a java program that calls the function this way:



      core_st[] cores = new core_st[1];
      MyLibrary.INSTANCE.open_core(0, cores);


      The function should populate cores[0] members with the result of the "open" operation. In particular, the two fields are two pointers to something else. What happens is that the void *instance field is always correctly populated, but the info field is always null (a pointer to zero).
      If I set the jna.memory_dump option to true, any call of core_st.toString() returns always the same result:



      memory dump
      [70cb64e7]
      [fd7f0000]
      [00000000]
      [00000000]


      It looks like the pointer to the info structure is not in the memory read by the JNA. The same call, performed by a similar C program, works fine, both pointers are correctly populated.
      I also tried to change the core_st mapping, just for test purposes:



      public class core_st extends Structure 

      public long instance;
      public long info;

      @Override
      protected List<String> getFieldOrder()
      return Arrays.asList("instance", "info");




      But I got no differences in the result. instance gets a non-null value, and info is always null. I am working with a 64bit VM.
      I was wondering if the problem could be the const modifier of the info field? Can the const modifier in a struct field of type pointer change the way the struct is stored in memory?







      java struct mapping jna






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 10:15







      Enrico

















      asked Mar 27 at 10:09









      EnricoEnrico

      1451 gold badge3 silver badges10 bronze badges




      1451 gold badge3 silver badges10 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0















          Can the const modifier in a struct field of type pointer change the way the struct is stored in memory?




          The answer is maybe, compiler dependent. More important to your question is how const affects the way the field is accessed on the native side. No matter what you do on the Java side, once you initialize the info field of your core_st structure, you can not modify it.



          And that is why you are seeing what you're seeing here: by defining public info_st.ByReference info; you are initializing the core_st structure with a pointer to NULL and getting the memory address 0x0 stored for that field. When accessing that field using the API, you can't change the memory address, it's stuck.



          You see the same results initializing it as a long with the default value (0).



          The solution depends on how the API populates that value. If, on the native side, the open_core function assumes that the info field is already initialized with a pointer to an allocated structure, and just changes the values at the pointed-to memory, you're all set. You just need to initialize that field when you first instantiate the structure.



          You don't tell me anything about what *info points to, so the answer is different depending on whether it's an array of info_st structures or a single structure. If it's a single structure, you have:



          public class core_st extends Structure 

          public Pointer instance;
          public info_st.ByReference info = new info_st.ByReference();




          This will allocate the appropriate memory here for an info_st structure and store the read-only pointer in your core_st structure. Depending on the inner workings of open_core() (and based on your report that this seems to work on the C side), this may work!



          If not, perhaps posting the working C code would help determine whether there's another JNA mapping that would help or if you have to work around it with your own custom dll wrapper function.






          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%2f55374608%2fis-there-a-specific-way-to-deal-with-const-pointer-members-of-a-struct-in-jna%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









            0















            Can the const modifier in a struct field of type pointer change the way the struct is stored in memory?




            The answer is maybe, compiler dependent. More important to your question is how const affects the way the field is accessed on the native side. No matter what you do on the Java side, once you initialize the info field of your core_st structure, you can not modify it.



            And that is why you are seeing what you're seeing here: by defining public info_st.ByReference info; you are initializing the core_st structure with a pointer to NULL and getting the memory address 0x0 stored for that field. When accessing that field using the API, you can't change the memory address, it's stuck.



            You see the same results initializing it as a long with the default value (0).



            The solution depends on how the API populates that value. If, on the native side, the open_core function assumes that the info field is already initialized with a pointer to an allocated structure, and just changes the values at the pointed-to memory, you're all set. You just need to initialize that field when you first instantiate the structure.



            You don't tell me anything about what *info points to, so the answer is different depending on whether it's an array of info_st structures or a single structure. If it's a single structure, you have:



            public class core_st extends Structure 

            public Pointer instance;
            public info_st.ByReference info = new info_st.ByReference();




            This will allocate the appropriate memory here for an info_st structure and store the read-only pointer in your core_st structure. Depending on the inner workings of open_core() (and based on your report that this seems to work on the C side), this may work!



            If not, perhaps posting the working C code would help determine whether there's another JNA mapping that would help or if you have to work around it with your own custom dll wrapper function.






            share|improve this answer































              0















              Can the const modifier in a struct field of type pointer change the way the struct is stored in memory?




              The answer is maybe, compiler dependent. More important to your question is how const affects the way the field is accessed on the native side. No matter what you do on the Java side, once you initialize the info field of your core_st structure, you can not modify it.



              And that is why you are seeing what you're seeing here: by defining public info_st.ByReference info; you are initializing the core_st structure with a pointer to NULL and getting the memory address 0x0 stored for that field. When accessing that field using the API, you can't change the memory address, it's stuck.



              You see the same results initializing it as a long with the default value (0).



              The solution depends on how the API populates that value. If, on the native side, the open_core function assumes that the info field is already initialized with a pointer to an allocated structure, and just changes the values at the pointed-to memory, you're all set. You just need to initialize that field when you first instantiate the structure.



              You don't tell me anything about what *info points to, so the answer is different depending on whether it's an array of info_st structures or a single structure. If it's a single structure, you have:



              public class core_st extends Structure 

              public Pointer instance;
              public info_st.ByReference info = new info_st.ByReference();




              This will allocate the appropriate memory here for an info_st structure and store the read-only pointer in your core_st structure. Depending on the inner workings of open_core() (and based on your report that this seems to work on the C side), this may work!



              If not, perhaps posting the working C code would help determine whether there's another JNA mapping that would help or if you have to work around it with your own custom dll wrapper function.






              share|improve this answer





























                0












                0








                0








                Can the const modifier in a struct field of type pointer change the way the struct is stored in memory?




                The answer is maybe, compiler dependent. More important to your question is how const affects the way the field is accessed on the native side. No matter what you do on the Java side, once you initialize the info field of your core_st structure, you can not modify it.



                And that is why you are seeing what you're seeing here: by defining public info_st.ByReference info; you are initializing the core_st structure with a pointer to NULL and getting the memory address 0x0 stored for that field. When accessing that field using the API, you can't change the memory address, it's stuck.



                You see the same results initializing it as a long with the default value (0).



                The solution depends on how the API populates that value. If, on the native side, the open_core function assumes that the info field is already initialized with a pointer to an allocated structure, and just changes the values at the pointed-to memory, you're all set. You just need to initialize that field when you first instantiate the structure.



                You don't tell me anything about what *info points to, so the answer is different depending on whether it's an array of info_st structures or a single structure. If it's a single structure, you have:



                public class core_st extends Structure 

                public Pointer instance;
                public info_st.ByReference info = new info_st.ByReference();




                This will allocate the appropriate memory here for an info_st structure and store the read-only pointer in your core_st structure. Depending on the inner workings of open_core() (and based on your report that this seems to work on the C side), this may work!



                If not, perhaps posting the working C code would help determine whether there's another JNA mapping that would help or if you have to work around it with your own custom dll wrapper function.






                share|improve this answer
















                Can the const modifier in a struct field of type pointer change the way the struct is stored in memory?




                The answer is maybe, compiler dependent. More important to your question is how const affects the way the field is accessed on the native side. No matter what you do on the Java side, once you initialize the info field of your core_st structure, you can not modify it.



                And that is why you are seeing what you're seeing here: by defining public info_st.ByReference info; you are initializing the core_st structure with a pointer to NULL and getting the memory address 0x0 stored for that field. When accessing that field using the API, you can't change the memory address, it's stuck.



                You see the same results initializing it as a long with the default value (0).



                The solution depends on how the API populates that value. If, on the native side, the open_core function assumes that the info field is already initialized with a pointer to an allocated structure, and just changes the values at the pointed-to memory, you're all set. You just need to initialize that field when you first instantiate the structure.



                You don't tell me anything about what *info points to, so the answer is different depending on whether it's an array of info_st structures or a single structure. If it's a single structure, you have:



                public class core_st extends Structure 

                public Pointer instance;
                public info_st.ByReference info = new info_st.ByReference();




                This will allocate the appropriate memory here for an info_st structure and store the read-only pointer in your core_st structure. Depending on the inner workings of open_core() (and based on your report that this seems to work on the C side), this may work!



                If not, perhaps posting the working C code would help determine whether there's another JNA mapping that would help or if you have to work around it with your own custom dll wrapper function.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 30 at 16:07

























                answered Mar 29 at 21:57









                Daniel WiddisDaniel Widdis

                2,3282 gold badges15 silver badges25 bronze badges




                2,3282 gold badges15 silver badges25 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    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%2f55374608%2fis-there-a-specific-way-to-deal-with-const-pointer-members-of-a-struct-in-jna%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

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript