These are the structs in C
public struct den_processing_result
{
public enum1 state;
public ulong bytesRead;
public ulong bytesWritten;
public unsafe den_context* context; <-----how to acquire this value
}
public struct den_conte...
Pulling the C tag because that's definitely NOT C code, leaving c++ for now, though I think c++-cli would be more appropriate.
user585968
@Dash "Please do not tagspam, [c] and [c++] are not related to this question" - incorrect. extern "C" __declspec(dllexport) is clearly c++. learn.microsoft.com/en-us/cpp/build/…
If I send IntPtr.Zero to C dll that would be null in this case?
user19858830
Ah sorry, i misread your question and subsequently your code. My apologies. Makes sense, i just couldn't pay attention...
user585968
01:10
If you have access to both the c++ and C# code, you might want to consider using C++/CLI for it's easier path for managed-to-unmanaged interop. Also, rather than expose everything to .NET, only expose what is needed. Try to encapsulate as much as you can on the c++/CLI side. e.g. is it really necessary to expose all the struct's contents? The raw memory blocks? etc
user19858830
public unsafe den_context* context; <-----how to acquire this value Who knows. You have only shown a native function that frees a context object. The DLL should also a have a function that allocates and provides a context object, no?
The structs on the C# side make some assumptions about the sizes and layout of the fields which I think won't be true, but it depends on how exactly the C++ side struct works, which is not clear: most of those types are non-standard. Normally fields are aligned to their natural alignment (which your C# struct assumes they aren't), and long (in C++, unlike in C#) is 4 bytes on windows. E: you're usually better off with sequential layout (typically does the right thing), unless you really need explicit layout and you know what you're doing
@MySkullCaveIsADarkPlace this is the function extern "C" __declspec(dllexport) void free_context(den_context *const context, void (*mem_free)(void *)) { if(mem_free == NULL) mem_free = free; if(!context->dictionary_type) mem_free(context->dictionary); mem_free(context); }
@harold Okay you're right. I tried with sequential (as default) I get the proper values of den_processing_result struct. But how can I get the values from den_context then?
@MySkullCaveIsADarkPlace Note that in source dll it says typedef unsigned long long which I believe it's 8-bytes if I'm not mistaken hence I used ulong which is 8 bytes
user19858830
But free_context frees an existing den_context, it doesn't create one. My previous comment was more in the direction: Since the DLL has a function for freeing a den_context, does it also have a function for creating some den_context?
@DarkKnight what do you mean by "get the values" exactly? You have some pointer _result.den_context and you can use it, right? Does the address look plausible?
@harold yes _result.den_context is the pointer to den_context. Now using that pointer how I can retrieve the data from the same struct? If that makes sense..
@MySkullCaveIsADarkPlace ah I didn't know that I read somewhere it said ulong takes 8 bytes. Does it matter if dll is compiled and targeted only for x64? Does it's value change?
@harold _result.den_context->algorithm in c#? How?
@MySkullCaveIsADarkPlace "does it also have a function for creating some den_context?" Yes it does. It returns den_make_result(STATE_OK, 0, 0, den_allocate_context(algorithm, custom_dictionary, mem_alloc));
enum1.ALGO1 is not part of the struct, it's just one of members of that enum. It doesn't make sense to pass that to free_context either, it expects a pointer (as IntPtr) to a context, so (IntPtr)Result.den_context
@harold so should I change the parameter of C dll from den_context *const context to IntPtr context ? Edit: I tried both ways console app still crashes. Hmph
No, IntPtr is a C# thing so you wouldn't be able to do that anyway. You could change the parameter to den_context *context on the C# side, then you can pass the pointer directly without casting it to IntPtr, doesn't really matter though.
@harold console app still crashes? Well at least how we can retrieve the data from both structs when one of their field is pointing to the second struct? I can get human readable data from den_processing_result dunno about den_context struct
IDK about that crash, but does for example result.context->dictionary_size give you any reasonable value? Even if it does though, dictionary is a void pointer so you can't really do much with it. The actual contents of the dictionary aren't directly usable.
Not particularly interested in dictionary value but after trying den_context context = (den_context)Marshal.PtrToStructure(new IntPtr(Result.context), typeof(den_context)); I get the values for den_context struct :D Well that's about getting the values from struct I don't understand why the console crashes when I use free_context in debug mode? and in release mode it doesn't execute next lines from the same method. Mystery