c# - Error looping through IntPtr to build array of struct -


i'm having problem building array of structs intptr in struct.

this structure returned windows api i'm using:

public struct dfs_info_9 {     [marshalas(unmanagedtype.lpwstr)]     public string entrypath;     [marshalas(unmanagedtype.lpwstr)]     public string comment;     public dfs_volume_state state;     public uint32 timeout;     public guid guid;     public uint32 propertyflags;     public uint32 metadatasize;     public uint32 sdlengthreserved;     public intptr psecuritydescriptor;     public uint32 numberofstorages;     public intptr storage; }  [dllimport("netapi32", charset = charset.auto, setlasterror = true)] public static extern int netdfsenum([marshalas(unmanagedtype.lpwstr)]string dfsname, int level, int prefmaxlen, out intptr buffer, [marshalas(unmanagedtype.i4)]out int entriesread, [marshalas(unmanagedtype.i4)]ref int resumehandle); 

i'm trying array of dfs_storage_info_1s referenced intptr storage.

here's structure (if matters):

public struct dfs_storage_info_1 {     public dfs_storage_state state;     [marshalas(unmanagedtype.lpwstr)]     public string servername;     [marshalas(unmanagedtype.lpwstr)]     public string sharename;     public intptr targetpriority; } 

so far code has been working dfs_info_9s 1 storage, fails when trying marshal second item in array.

dfs_info_9 info = getinfofromwinapi(); list<dfs_storage_info_1> storages = new list<dfs_storage_info_1>(); (int = 0; < info.numberofstorages; i++) {     intptr pstorage = new intptr(info.storage.toint64() + * marshal.sizeof(typeof(dfs_storage_info_1)));     dfs_storage_info_1 storage = (dfs_storage_info_1)marshal.ptrtostructure(pstorage, typeof(dfs_storage_info_1));     storages.add(storage); } 

i'm getting fatalexecutionengineerror spits out error code 0x0000005 (access denied). i'm assuming size of dfs_storage_info_1 being miscalculated causing marshal attempt access memory outside allocated array. happening on i = 1, when there might 7 storages through. maybe thinking flawed, have no idea how rectify this.

the actual definition of dfs_storage_info_1 this:

public struct dfs_storage_info_1 {     public dfs_storage_state state;     [marshalas(unmanagedtype.lpwstr)]     public string servername;     [marshalas(unmanagedtype.lpwstr)]     public string sharename;     dfs_target_priority targetpriority; } 

targetpriority structure, defined here:

public struct dfs_target_priority {     public dfs_target_priority_class targetpriorityclass;     public uint16 targetpriorityrank;     public uint16 reserved; }  public enum dfs_target_priority_class {     dfsinvalidpriorityclass = -1,     dfssitecostnormalpriorityclass = 0,     dfsglobalhighpriorityclass = 1,     dfssitecosthighpriorityclass = 2,     dfssitecostlowpriorityclass = 3,     dfsgloballowpriorityclass = 4 } 

as fatalexecutionengineerror, believe size of structure dfs_storage_info_1 being miscalculated since defined incorrectly. when trying convert pointers structures referenced, next index wrong because size off. when converting block of memory, presumably referenced block shouldn't have been accessible, throwing "access denied (0x0000005)" error.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -