Get SizeConst value for array in struct












1















I have some (generated) c++ code that declares the following struct in a DLL:



[StructLayout(LayoutKind::Sequential, Pack = 4)] public value struct typeData
{
public:
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 8)] array<UINT8> ^ a1;
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 20)] array<UINT8> ^ a2;
};


From my C# application, I would like to find out the expected lengths of the arrays (8 and 20 in the example).



I have loaded the DLL, and created an instance of the object as



dll = Assembly.LoadFrom("data.dll");
var t = dll.GetType("typeData");
var obj = Activator.CreateInstance(t);


Can I get the desired information from the type or the object? Or in some other way?










share|improve this question























  • These declarations nail down the view that native code sees. After the pinvoke marshaler is done with it you get the managed struct, these become plain arrays you use every day. With a Length property that will be 8 and 20. That doesn't happen until you actually make the call. It is not like you can't dig it out with Reflection, FieldInfo.GetCustomAttribute(), it is merely painful and almost certainly not needed. Also do keep in mind that this is pretty unusual in C++/CLI, you'd simply use the native struct type you got from the .h file to make the call.

    – Hans Passant
    Nov 26 '18 at 13:15











  • Hmm, which call are you referring to? There is no native code for me to call in this case. I only have the value struct declaration in the dll. I would use this information from my C# application to interpret binary data that has been generated by a different native application. And also to verify that the individual array sizes in the struct definition corresponds to what my application expects.

    – matli
    Nov 26 '18 at 15:04











  • Whatever call that generated this struct. Usually, it is sometimes also used to interpret binary data in a file or stream, takes Marshal.PtrToStructure(). The outcome is not very different, you get the managed struct back with the arrays at the size you'd want them, their Length property is not going to be a surprise. I do need to guess when you ask about a solution without describing the problem it is supposed to solve.

    – Hans Passant
    Nov 26 '18 at 15:14













  • Ok, I know it's difficult to guess my situation. It is also quite difficult to describe the system without making huge simplifications. However, in the most basic case, let's say the only thing I have is the DLL as described, containing the value struct definition. I also have a specification, from another source, telling me that the struct should contain one 8-byte field and one 20-byte field, in that order. I want to be able to verify that the struct as defined in the DLL corresponds to this.

    – matli
    Nov 26 '18 at 15:27











  • If the native declaration does not match then you get garbage data. That's never very hard to detect with human eyes powered by a debugger. No, there is no way to discover the native declaration if you don't use the .h file. Native code does not have anything resembling reflection. Since this appears to be auto-generated, SWIG perhaps, there isn't a lot of reason to assume there is a mismatch.

    – Hans Passant
    Nov 26 '18 at 15:35
















1















I have some (generated) c++ code that declares the following struct in a DLL:



[StructLayout(LayoutKind::Sequential, Pack = 4)] public value struct typeData
{
public:
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 8)] array<UINT8> ^ a1;
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 20)] array<UINT8> ^ a2;
};


From my C# application, I would like to find out the expected lengths of the arrays (8 and 20 in the example).



I have loaded the DLL, and created an instance of the object as



dll = Assembly.LoadFrom("data.dll");
var t = dll.GetType("typeData");
var obj = Activator.CreateInstance(t);


Can I get the desired information from the type or the object? Or in some other way?










share|improve this question























  • These declarations nail down the view that native code sees. After the pinvoke marshaler is done with it you get the managed struct, these become plain arrays you use every day. With a Length property that will be 8 and 20. That doesn't happen until you actually make the call. It is not like you can't dig it out with Reflection, FieldInfo.GetCustomAttribute(), it is merely painful and almost certainly not needed. Also do keep in mind that this is pretty unusual in C++/CLI, you'd simply use the native struct type you got from the .h file to make the call.

    – Hans Passant
    Nov 26 '18 at 13:15











  • Hmm, which call are you referring to? There is no native code for me to call in this case. I only have the value struct declaration in the dll. I would use this information from my C# application to interpret binary data that has been generated by a different native application. And also to verify that the individual array sizes in the struct definition corresponds to what my application expects.

    – matli
    Nov 26 '18 at 15:04











  • Whatever call that generated this struct. Usually, it is sometimes also used to interpret binary data in a file or stream, takes Marshal.PtrToStructure(). The outcome is not very different, you get the managed struct back with the arrays at the size you'd want them, their Length property is not going to be a surprise. I do need to guess when you ask about a solution without describing the problem it is supposed to solve.

    – Hans Passant
    Nov 26 '18 at 15:14













  • Ok, I know it's difficult to guess my situation. It is also quite difficult to describe the system without making huge simplifications. However, in the most basic case, let's say the only thing I have is the DLL as described, containing the value struct definition. I also have a specification, from another source, telling me that the struct should contain one 8-byte field and one 20-byte field, in that order. I want to be able to verify that the struct as defined in the DLL corresponds to this.

    – matli
    Nov 26 '18 at 15:27











  • If the native declaration does not match then you get garbage data. That's never very hard to detect with human eyes powered by a debugger. No, there is no way to discover the native declaration if you don't use the .h file. Native code does not have anything resembling reflection. Since this appears to be auto-generated, SWIG perhaps, there isn't a lot of reason to assume there is a mismatch.

    – Hans Passant
    Nov 26 '18 at 15:35














1












1








1








I have some (generated) c++ code that declares the following struct in a DLL:



[StructLayout(LayoutKind::Sequential, Pack = 4)] public value struct typeData
{
public:
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 8)] array<UINT8> ^ a1;
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 20)] array<UINT8> ^ a2;
};


From my C# application, I would like to find out the expected lengths of the arrays (8 and 20 in the example).



I have loaded the DLL, and created an instance of the object as



dll = Assembly.LoadFrom("data.dll");
var t = dll.GetType("typeData");
var obj = Activator.CreateInstance(t);


Can I get the desired information from the type or the object? Or in some other way?










share|improve this question














I have some (generated) c++ code that declares the following struct in a DLL:



[StructLayout(LayoutKind::Sequential, Pack = 4)] public value struct typeData
{
public:
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 8)] array<UINT8> ^ a1;
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 20)] array<UINT8> ^ a2;
};


From my C# application, I would like to find out the expected lengths of the arrays (8 and 20 in the example).



I have loaded the DLL, and created an instance of the object as



dll = Assembly.LoadFrom("data.dll");
var t = dll.GetType("typeData");
var obj = Activator.CreateInstance(t);


Can I get the desired information from the type or the object? Or in some other way?







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 12:58









matlimatli

14.3k62935




14.3k62935













  • These declarations nail down the view that native code sees. After the pinvoke marshaler is done with it you get the managed struct, these become plain arrays you use every day. With a Length property that will be 8 and 20. That doesn't happen until you actually make the call. It is not like you can't dig it out with Reflection, FieldInfo.GetCustomAttribute(), it is merely painful and almost certainly not needed. Also do keep in mind that this is pretty unusual in C++/CLI, you'd simply use the native struct type you got from the .h file to make the call.

    – Hans Passant
    Nov 26 '18 at 13:15











  • Hmm, which call are you referring to? There is no native code for me to call in this case. I only have the value struct declaration in the dll. I would use this information from my C# application to interpret binary data that has been generated by a different native application. And also to verify that the individual array sizes in the struct definition corresponds to what my application expects.

    – matli
    Nov 26 '18 at 15:04











  • Whatever call that generated this struct. Usually, it is sometimes also used to interpret binary data in a file or stream, takes Marshal.PtrToStructure(). The outcome is not very different, you get the managed struct back with the arrays at the size you'd want them, their Length property is not going to be a surprise. I do need to guess when you ask about a solution without describing the problem it is supposed to solve.

    – Hans Passant
    Nov 26 '18 at 15:14













  • Ok, I know it's difficult to guess my situation. It is also quite difficult to describe the system without making huge simplifications. However, in the most basic case, let's say the only thing I have is the DLL as described, containing the value struct definition. I also have a specification, from another source, telling me that the struct should contain one 8-byte field and one 20-byte field, in that order. I want to be able to verify that the struct as defined in the DLL corresponds to this.

    – matli
    Nov 26 '18 at 15:27











  • If the native declaration does not match then you get garbage data. That's never very hard to detect with human eyes powered by a debugger. No, there is no way to discover the native declaration if you don't use the .h file. Native code does not have anything resembling reflection. Since this appears to be auto-generated, SWIG perhaps, there isn't a lot of reason to assume there is a mismatch.

    – Hans Passant
    Nov 26 '18 at 15:35



















  • These declarations nail down the view that native code sees. After the pinvoke marshaler is done with it you get the managed struct, these become plain arrays you use every day. With a Length property that will be 8 and 20. That doesn't happen until you actually make the call. It is not like you can't dig it out with Reflection, FieldInfo.GetCustomAttribute(), it is merely painful and almost certainly not needed. Also do keep in mind that this is pretty unusual in C++/CLI, you'd simply use the native struct type you got from the .h file to make the call.

    – Hans Passant
    Nov 26 '18 at 13:15











  • Hmm, which call are you referring to? There is no native code for me to call in this case. I only have the value struct declaration in the dll. I would use this information from my C# application to interpret binary data that has been generated by a different native application. And also to verify that the individual array sizes in the struct definition corresponds to what my application expects.

    – matli
    Nov 26 '18 at 15:04











  • Whatever call that generated this struct. Usually, it is sometimes also used to interpret binary data in a file or stream, takes Marshal.PtrToStructure(). The outcome is not very different, you get the managed struct back with the arrays at the size you'd want them, their Length property is not going to be a surprise. I do need to guess when you ask about a solution without describing the problem it is supposed to solve.

    – Hans Passant
    Nov 26 '18 at 15:14













  • Ok, I know it's difficult to guess my situation. It is also quite difficult to describe the system without making huge simplifications. However, in the most basic case, let's say the only thing I have is the DLL as described, containing the value struct definition. I also have a specification, from another source, telling me that the struct should contain one 8-byte field and one 20-byte field, in that order. I want to be able to verify that the struct as defined in the DLL corresponds to this.

    – matli
    Nov 26 '18 at 15:27











  • If the native declaration does not match then you get garbage data. That's never very hard to detect with human eyes powered by a debugger. No, there is no way to discover the native declaration if you don't use the .h file. Native code does not have anything resembling reflection. Since this appears to be auto-generated, SWIG perhaps, there isn't a lot of reason to assume there is a mismatch.

    – Hans Passant
    Nov 26 '18 at 15:35

















These declarations nail down the view that native code sees. After the pinvoke marshaler is done with it you get the managed struct, these become plain arrays you use every day. With a Length property that will be 8 and 20. That doesn't happen until you actually make the call. It is not like you can't dig it out with Reflection, FieldInfo.GetCustomAttribute(), it is merely painful and almost certainly not needed. Also do keep in mind that this is pretty unusual in C++/CLI, you'd simply use the native struct type you got from the .h file to make the call.

– Hans Passant
Nov 26 '18 at 13:15





These declarations nail down the view that native code sees. After the pinvoke marshaler is done with it you get the managed struct, these become plain arrays you use every day. With a Length property that will be 8 and 20. That doesn't happen until you actually make the call. It is not like you can't dig it out with Reflection, FieldInfo.GetCustomAttribute(), it is merely painful and almost certainly not needed. Also do keep in mind that this is pretty unusual in C++/CLI, you'd simply use the native struct type you got from the .h file to make the call.

– Hans Passant
Nov 26 '18 at 13:15













Hmm, which call are you referring to? There is no native code for me to call in this case. I only have the value struct declaration in the dll. I would use this information from my C# application to interpret binary data that has been generated by a different native application. And also to verify that the individual array sizes in the struct definition corresponds to what my application expects.

– matli
Nov 26 '18 at 15:04





Hmm, which call are you referring to? There is no native code for me to call in this case. I only have the value struct declaration in the dll. I would use this information from my C# application to interpret binary data that has been generated by a different native application. And also to verify that the individual array sizes in the struct definition corresponds to what my application expects.

– matli
Nov 26 '18 at 15:04













Whatever call that generated this struct. Usually, it is sometimes also used to interpret binary data in a file or stream, takes Marshal.PtrToStructure(). The outcome is not very different, you get the managed struct back with the arrays at the size you'd want them, their Length property is not going to be a surprise. I do need to guess when you ask about a solution without describing the problem it is supposed to solve.

– Hans Passant
Nov 26 '18 at 15:14







Whatever call that generated this struct. Usually, it is sometimes also used to interpret binary data in a file or stream, takes Marshal.PtrToStructure(). The outcome is not very different, you get the managed struct back with the arrays at the size you'd want them, their Length property is not going to be a surprise. I do need to guess when you ask about a solution without describing the problem it is supposed to solve.

– Hans Passant
Nov 26 '18 at 15:14















Ok, I know it's difficult to guess my situation. It is also quite difficult to describe the system without making huge simplifications. However, in the most basic case, let's say the only thing I have is the DLL as described, containing the value struct definition. I also have a specification, from another source, telling me that the struct should contain one 8-byte field and one 20-byte field, in that order. I want to be able to verify that the struct as defined in the DLL corresponds to this.

– matli
Nov 26 '18 at 15:27





Ok, I know it's difficult to guess my situation. It is also quite difficult to describe the system without making huge simplifications. However, in the most basic case, let's say the only thing I have is the DLL as described, containing the value struct definition. I also have a specification, from another source, telling me that the struct should contain one 8-byte field and one 20-byte field, in that order. I want to be able to verify that the struct as defined in the DLL corresponds to this.

– matli
Nov 26 '18 at 15:27













If the native declaration does not match then you get garbage data. That's never very hard to detect with human eyes powered by a debugger. No, there is no way to discover the native declaration if you don't use the .h file. Native code does not have anything resembling reflection. Since this appears to be auto-generated, SWIG perhaps, there isn't a lot of reason to assume there is a mismatch.

– Hans Passant
Nov 26 '18 at 15:35





If the native declaration does not match then you get garbage data. That's never very hard to detect with human eyes powered by a debugger. No, there is no way to discover the native declaration if you don't use the .h file. Native code does not have anything resembling reflection. Since this appears to be auto-generated, SWIG perhaps, there isn't a lot of reason to assume there is a mismatch.

– Hans Passant
Nov 26 '18 at 15:35












0






active

oldest

votes












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%2f53481651%2fget-sizeconst-value-for-array-in-struct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53481651%2fget-sizeconst-value-for-array-in-struct%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

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen