Converting C structures to C# equivalents...
-
Hi all, What is the best way to convert C style structures to C#. I have converted the following C structure typedef struct { ISC_DATE timestamp_date; ISC_TIME timestamp_time; } ISC_TIMESTAMP; to public struct ISC_TIMESTAMP { long TIMESTAMP_DATE; ulong TIMESTAMP_TIME; }; Is this correct or should I be using another more C# style like.. [StructLayout(LayoutKind.Sequential)] public class ISC_TIMESTAMP { // rest of code goes here } How about this more complicated C structure... typedef struct blobcallback { short (ISC_FAR *blob_get_segment) (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size, ISC_USHORT* result_len); void ISC_FAR *blob_handle; ISC_LONG blob_number_segments; ISC_LONG blob_max_segment; ISC_LONG blob_total_length; void (ISC_FAR *blob_put_segment) (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size); ISC_LONG (ISC_FAR *blob_lseek) (void ISC_FAR* hnd, ISC_USHORT mode, ISC_LONG offset); } ISC_FAR *BLOBCALLBACK; What is the best way to handle this type of conversion. Sincerely, Dominique.
-
Hi all, What is the best way to convert C style structures to C#. I have converted the following C structure typedef struct { ISC_DATE timestamp_date; ISC_TIME timestamp_time; } ISC_TIMESTAMP; to public struct ISC_TIMESTAMP { long TIMESTAMP_DATE; ulong TIMESTAMP_TIME; }; Is this correct or should I be using another more C# style like.. [StructLayout(LayoutKind.Sequential)] public class ISC_TIMESTAMP { // rest of code goes here } How about this more complicated C structure... typedef struct blobcallback { short (ISC_FAR *blob_get_segment) (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size, ISC_USHORT* result_len); void ISC_FAR *blob_handle; ISC_LONG blob_number_segments; ISC_LONG blob_max_segment; ISC_LONG blob_total_length; void (ISC_FAR *blob_put_segment) (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size); ISC_LONG (ISC_FAR *blob_lseek) (void ISC_FAR* hnd, ISC_USHORT mode, ISC_LONG offset); } ISC_FAR *BLOBCALLBACK; What is the best way to handle this type of conversion. Sincerely, Dominique.
Dominique wrote:_
public struct ISC_TIMESTAMP
{
long TIMESTAMP_DATE;
ulong TIMESTAMP_TIME;
};Is this correct or should I be using another more C# style like..
[StructLayout(LayoutKind.Sequential)]
public class ISC_TIMESTAMP
{
// rest of code goes here
}You need to use a combination of the two, keep it a struct but apply the StructLayout attribute its your only guarantee that the data will stay in that order; otherwise the compiler could move fields around to better align the data. Dominique wrote:
struct blobcallback {
short (ISC_FAR *blob_get_segment)
(void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size, ISC_USHORT* result_len);
void ISC_FAR *blob_handle;
ISC_LONG blob_number_segments;
ISC_LONG blob_max_segment;
ISC_LONG blob_total_length;
void (ISC_FAR *blob_put_segment)
(void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size);
ISC_LONG (ISC_FAR *blob_lseek)
(void ISC_FAR* hnd, ISC_USHORT mode, ISC_LONG offset);
}_Call backs are done with delegates, there is an article somewhere on CP that shows how to use a callback for the EnumWindows function, i think it was how to programmatcally control IE. void is usually a
object
When you have a character buffer pass in a StringBuilder object A ** (pointer to a pointer) usually uses the 'out' keyword followed by its type, a pointer is usually a reference ('ref'). HTH, James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971