www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C to D struct including LPVOID,LPCSTR

reply BLS <nanali wanadoo.fr> writes:
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;
......

I think the D version might look  similar to :

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

alias stHFFileCtx * pstHFFileCtx;

Two questions :
How do I translate : LPCSTR
What else is wrong.
Sorry for asking you such silly questions and thanks in advance.
Björn
Sep 09 2006
next sibling parent BLS <nanali wanadoo.fr> writes:
I forget :
LPCSTR is a Winapi typedef :
long pointer to a constant unicode portable string (or something in 
between , or what the heck <g>)
Björn



BLS schrieb:
 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;
 ......
 
 I think the D version might look  similar to :
 
 struct stHFFileCtx {
 align(1):            //
     void* pData;        /*pointeur vers une structure d'enreg*/
     short nSize;        /*taille de la structure d'enreg*/
     LPCSTR pszNomFic;    /*nom logique du fichier associй*/
     void* pStorage;        /*copie de la structure d'enreg*/
 }    ;
 
 alias stHFFileCtx * pstHFFileCtx;
 
 Two questions :
 How do I translate : LPCSTR
 What else is wrong.
 Sorry for asking you such silly questions and thanks in advance.
 Björn
Sep 09 2006
prev sibling next sibling parent reply Steve Horne <stephenwantshornenospam100 aol.com> writes:
On Sat, 09 Sep 2006 22:53:06 +0200, BLS <nanali wanadoo.fr> wrote:

How do I translate : LPCSTR
That means 'pointer to a C string'. One possible answer is char*. D can handle C strings. If you do this, the pointer points to the first character of the string which is null terminated. It is possible to access the C string library from D, IIRC, but you have the same responsibilities as you would in C, like making sure the memory allocated for the string is big enough. With Windows API stuff, you're probably stuck with this. If you can change the type of the string, though, consider using char[] - a D string. It still operates as an array of characters, but D arrays are better. IIRC there is an implicit pointer in there (the string itself wouldn't end up in the struct) so the struct will still be the same size.
What else is wrong.
I hate that C/C++ compatibility typedef/struct syntax. Confuses me every time - I start thinking of the syntax where you declare a struct and an instance of that struct in one go :-( I think you're OK, though. -- Remove 'wants' and 'nospam' from e-mail.
Sep 10 2006
parent reply nobody <nobody mailinator.com> writes:
Steve Horne wrote:
 On Sat, 09 Sep 2006 22:53:06 +0200, BLS <nanali wanadoo.fr> wrote:
 
 How do I translate : LPCSTR
If you can change the type of the string, though, consider using char[] - a D string. It still operates as an array of characters, but D arrays are better. IIRC there is an implicit pointer in there (the string itself wouldn't end up in the struct) so the struct will still be the same size.
Just wanted to point out that D strings are structs with a pointer and a length. C strings are just a pointer. So the size of a D string is twice that of a C string which would change the size of the struct.
Sep 10 2006
parent Steve Horne <stephenwantshornenospam100 aol.com> writes:
On Sun, 10 Sep 2006 09:35:39 -0400, nobody <nobody mailinator.com>
wrote:

Just wanted to point out that D strings are structs with a pointer and a
length. 
C strings are just a pointer. So the size of a D string is twice that of a C 
string which would change the size of the struct.
OK - thanks. I assumed the length went with the string itself, but thinking about it, that would break those printf examples I saw somewhere. -- Remove 'wants' and 'nospam' from e-mail.
Sep 10 2006
prev sibling parent reply nobody <nobody mailinator.com> writes:
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;
Sep 10 2006
parent BLS <nanali wanadoo.fr> writes:
Many thanks, compiles.
Björn
Sep 11 2006