Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create static method for structures which require initialization #8

Closed
NN--- opened this issue Sep 4, 2018 · 7 comments
Closed

Create static method for structures which require initialization #8

NN--- opened this issue Sep 4, 2018 · 7 comments
Assignees
Projects

Comments

@NN---
Copy link
Contributor

NN--- commented Sep 4, 2018

PInvoke library has a convenient Create method for structures where there is a need for some default initialization.
This library should have this too.

For instance:
https://github.com/AArnott/pinvoke/blob/bc4c6830f1918f5af28e76e075dd0750f27d3c61/src/Kernel32/desktop/Kernel32%2BStartupInfo.cs#L127

@dahall
Copy link
Owner

dahall commented Sep 4, 2018

I can definitely do this for this particular structure. I have done it elsewhere in the library as constructors and would likely follow that same pattern here.

@NN---
Copy link
Contributor Author

NN--- commented Sep 4, 2018

The problem is that you cannot overload empty constructor in struct, so using static method is a better way IMO.

@dahall dahall self-assigned this Sep 5, 2018
@dahall dahall added this to In progress in PInvoke Sep 25, 2018
@chrpai
Copy link

chrpai commented Jun 27, 2019

I think this would help me also. My P/Invoke isn't that strong and I'm trying to get my feet wet with Authz API by calling AuthzEnumerateSecurityEventSources. I call it once to get back a count of 12
then initialize an array of AUTHZ_SOURCE_SCHEMA_REGISTRATION with that many elements. However I think I'm doing something wrong because when I call Marshal.SizeOf() I get an error saying it cannot be marshaled as an unmanaged structurel no meaninful size of offset can be computed. I've seen StackOverflow answers talking about making sure the struct has the attribute [StructLayout(LayoutKind.Sequential)] and looking through Vanara code it does.

@chrpai
Copy link

chrpai commented Jun 27, 2019

I think I found what I needed in the AuthzEnumerateSecurityEventSourcesTest method but I'm not sure it's best practice to just assume 200 is big enough. I was assuming I'd have to call the API once to get the count and then allocate that many. But I don't really know this API well enough to say that for a fact.

@dahall
Copy link
Owner

dahall commented Jun 28, 2019

I need to spend some time investigating this, but my guess is that it is an error on my part. I'm traveling this week and may not be able to validate or invalidate this very quickly.

@dahall
Copy link
Owner

dahall commented Jul 1, 2019

@chrpai Please see the new issue I created from your last message. There is no bug and I provided and example of how to use the method without overallocating memory.

@dahall
Copy link
Owner

dahall commented Oct 3, 2019

I'm closing this as I have been adding a static readonly Default field for most structures that require initialization of any type. I'll continue this trend as I add and update. Thanks for the push.

@dahall dahall closed this as completed Oct 3, 2019
PInvoke automation moved this from In progress to Done Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PInvoke
  
Done
Development

No branches or pull requests

3 participants