0

I need your help.

I have the following c Code as a dll ( cant recompile or change it ) :

 typedef struct {
/* The name of the test */
char *name;

/* The SHORT name of the test (its call name) */
char *sname;

/* pointer to a test description */
char *description;

/* Standard test default */
unsigned int psamples_std;

/* Standard test default */
unsigned int tsamples_std;

/* Number of independent statistics generated per run */
unsigned int nkps;

/* A pointer to the test itself (must be filled at initialization) */
int (*test)();

/* void pointer to a vector of additional test arguments */
void *targs;

} Dtest;

This struct is used inside the dll in an array as you can see here:

 Dtest *dh_test_types[1000];

After a lot of research I found a way to get "simple type" varibales out of the DLL via:

IntPtr hdl;
hdl = LoadLibrary("cygdieharder-3.dll");
IntPtr addr = GetProcAddress(hdl, "dh_test_types");

And for simple types i used

int value = Marshal.ReadInt32(addr);

But I cant get any Information about the array of "Dtest" structures inside the URL because there is nothing like Marshal.readDtest(addr)

It would be great if someone could help me.

Thanking you in anticipation

Reallyor
  • 37
  • 9
  • Did you look into PInvoke? – Matt Apr 21 '14 at 15:47
  • What exactly do you mean ? I am executing some FUNCTIONS via PInvoke, but in this case I need a variable ( array of Dtest structs ). Or can I also get variables via PInkvoke ? – Reallyor Apr 21 '14 at 15:59
  • yes, struct can be returned via PInvoke – Matt Apr 21 '14 at 16:04
  • But I do not have a getter Method to get the array of structs, I Just know that the dll is using this dh_test_types intern and my dependency walker is showing me this name. – Reallyor Apr 21 '14 at 16:19
  • if the variables is not exposed, why do you want to access it in C#? – Matt Apr 21 '14 at 17:08
  • I am not sure whether the variable is exposed or not. According to your hint I tried to PInvoke a function called after the variable "dh_test_types()" because Dependency walker is showing the variable as a function. But i am getting an Marshalling exception now. I think i did something wrong in marshalling an array of the Dtest data typ. Do you know how to marshall it correctly ? Ps: to answer your question ... i need one of these Dtests as an argument for another function in the dll – Reallyor Apr 21 '14 at 17:15
  • Check this article: http://msdn.microsoft.com/en-us/library/ef4c3t39.aspx. Also, there're many posts here on how to marshal the struct from C++ to C# – Matt Apr 21 '14 at 17:43

1 Answers1

0

Given that your struct is defined as above, and the variable:

Dtest *dh_test_types[1000];  

is defined from that typedef, then an array of that pointer to struct (with 100 elements) can be accessed this way:

    dh_test_types[0]->name;
    dh_test_types[0]->sname;
    dh_test_types[0]->description;
    dh_test_types[0]->psamples_std;
    dh_test_types[0]->sname;
    dh_test_types[0]->targs;
    dh_test_types[0]->test;
    dh_test_types[0]->tsamples_std;  
//...  all the rest of indices...
    dh_test_types[99]->name;
    dh_test_types[99]->sname;
    dh_test_types[99]->description;
    dh_test_types[99]->psamples_std;
    dh_test_types[99]->sname;
    dh_test_types[99]->targs;
    dh_test_types[99]->test;
    dh_test_types[99]->tsamples_std;   

And everything in between

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • I do not even know what "->" should be ... is this a regular c# operation? And my problem is that the dh_test_types is inside the dll, so i cant just access the array. but I get an address as IntPtr. – Reallyor Apr 21 '14 at 16:13
  • Accessing struct members in pointer form requires using `->` notation. Non-pointer uses `.` Because of the array construct, the `->` is required. Read more ***[HERE](http://stackoverflow.com/questions/9072688/dot-operator-and-arrow-operator-use-in-c-vs-objective-c)***. ***However***, in looking at your original post, I am not sure this will help you. You are trying to access information contained in a process that is not necessarily exported, unless you can find a function to read that `struct` out, then you might be toast. – ryyker Apr 21 '14 at 17:23