C to D struct including LPVOID,LPCSTR

nobody nobody at mailinator.com
Sun Sep 10 09:00:47 PDT 2006


BLS wrote:
> Hi folks,
> as announced in my previous post, I am quit weak in C-ish languages.  I 
> have to translate the following CPP structure into D :
> 
> #pragma pack(1)
> 
> /*Contexte d'enreg pour un fichier*/
> typedef struct _stHFFileCtx {
>     LPVOID pData;        /*pointeur vers une structure d'enreg*/
>     short nSize;        /*taille de la structure d'enreg*/
>     LPCSTR pszNomFic;    /*nom logique du fichier associй*/
>     LPVOID pStorage;    /*copie de la structure d'enreg*/
> }    stHFFileCtx;
> 
> typedef stHFFileCtx far * pstHFFileCtx;

These types are defined in std.c.windows.windows as:

   alias void *LPVOID;
   alias char CHAR;
   alias CHAR *LPCSTR;
   alias LPCSTR LPCTSTR;

You were very close to the right translation but you want the 'align' to go just 
in front of the struct. I believe what your version says is that the compiler 
should align the pData field on a 1 byte boundary. Also in D the pointer star 
usually goes right next to the type. So now we have:

   align(1):
   struct stHFFileCtx {
       void* pData;        /*pointeur vers une structure d'enreg*/
       short nSize;        /*taille de la structure d'enreg*/
       char* pszNomFic;    /*nom logique du fichier associй*/
       void* pStorage;        /*copie de la structure d'enreg*/
   };

   alias stHFFileCtx* pstHFFileCtx;

The translated version comes with the caveat that you must make sure the string 
data is null terminated. You might also want to just import the windows file 
which would make the translation much closer to the original:

   public import std.c.windows.windows;
   align(1):
   struct stHFFileCtx {
       LPVOID pData;        /*pointeur vers une structure d'enreg*/
       short nSize;        /*taille de la structure d'enreg*/
       LPCSTR pszNomFic;    /*nom logique du fichier associй*/
       LPVOID pStorage;        /*copie de la structure d'enreg*/
   };

   alias stHFFileCtx* pstHFFileCtx;



More information about the Digitalmars-d-learn mailing list