D-DLLs & Python

Chris wendlec at tcd.ie
Wed Feb 20 03:06:24 PST 2013


On Tuesday, 19 February 2013 at 17:40:03 UTC, John Colvin wrote:
>
> D doesn't use null termination for it's strings, strings are 
> immutable(char)[]. You can form a D slice from a pointer by 
> going
> slice = ptr[0..length]
> where length is the length of the array the pointer represents.
> You can't just take a c style string and expect writeln to work 
> with it.


I had tried to!string() and the slicing already but to no avail. 
I think it has something to do with pointer and the stack. Here 
is what I get

export void printThis(ref char[] str) {
	printf("Incoming printf: %s\n", str);
	writeln(str);
	writeln(str.ptr);
	writeln(str[0..5]);
	writeln(to!string(str.ptr));
	writeln(to!string(str.ptr[0..5]));
	writeln(to!string(str)); // Error!
}


OUTPUT:

Incoming printf: Hello world
☺
27FBCC
☺
☺
☺
Traceback (most recent call last):
   File "loaddll.py", line 6, in <module>
     lib.printThis(c_char_p("Hello world"))
WindowsError: exception: access violation reading 0x00284000

If I use writefln("%s\n", str) I get the full garbage which 
starts with

☺   ☺   0³' ░ï^ ♦   ☺       (←5     8¹' l¹' x ' x ' 1þ→↔A±ôj    
░³' .ö→↔☺◄  D0 ►
ô→↔ æ"☻­C←↔Jô→↔È1←↔ ß"☻¶)"☻    °;5     +Æ→↔á╚h P®h P®h `³'     ►  
  P³' 0³' ░ï^ p
³'     ☺   a↨»wxY#☻êS→↔D0 ►P®h         ☺◄      ░<$☻    á╚h 
►Q→↔á╚h         xY#☻
    ░<$☻    D0 ►            \¬▲    P®h         ─²' ¬Ø♫▲á╚h P®h     
á╚h ☺   ☺
   P®h EÑ♫▲á╚h ☺   ☺    æ"☻╚Ui Ó("☻hj] └Ui ╣¢♫▲Ô╚♫▲─²' ­½] ­½] └Ui 
   '  æ"☻Ó("☻
   fÉ"☻☺   4É"☻    ÞJh (←5     á*♥▲­½] └Ui     H▒        ▲─Ui ♥   
            p■]
      ıÏ♫▲êTi     ­½] ú/5   ' (Ô$☻+¯♫▲ê♀"☻ú/5     └Ui (←5 2┘♫▲­½] 
êTi ­&^

3◄▲­½] ­&^ ­&^ (Ô$☻Ó┌5 ED◄▲(Ô$☻­&^ ­&^ ☺   ú/5ttt®/5 ¤I◄tttú/5 ☺☺ 
  ­&^ ­&^ ☺
 ' ☺ tttú/5 ▬☻nt­&^ ☺   ¨Rtttú/5 ☺     ' ðÚmttt§ç♥tttú/5 ☺     ' 
|3 ↔☺   ê '
  ú/5       ttt                              Âü☺           !☻  
+e▲Q    ╚¬$Q    +e
▲Q    m◄ ↔☻   É/5 ÿ 5 í9┐w         Ó²~        X ' ×⌂P¢─ ' U▬ 
↔±þÿj    ö ' ¬3╚v Ó
²~È ' ‗×┐w Ó²~M∟ºw         Ó²~            á '         ıq├w®&>     
ý ' ┼×┐w¶‼ ↔ Ó
²~                ¶‼ ↔ Ó²~    Actx    ☺   ♀3  ▄               ¶   
☺      4   |☺
  ☺                       ☻   N´&→ÿ☻  D   Ó☻  `☻      ║q2¾@♣  J   
î♣  ▲♥      [IY
   2      ♥      ═Û╬2Ó♂  B   $♀  6♥      ╚_P8\☼  ^   ╝☼  h♥      
D♣(▒$‼  V   |‼
ÿ♥  ►              ý   ☻   ☺   |☺  ð§  ☺   ☻   L↨  á  ☺   ♥   ý▲  
î♫  ☺   ♦   x-
   ¶♥  ☻   ♣   î0  ÿ   ☻   ♠   $1  ╠   ☻      ­1  ­   ☺            
  Ó2  (   ☻   ♂
  3  ♦   ☺   SsHd,   ☺   ☺   ☺   ♠   î   ☺   ÿ§  ,   ^   ^
                 ☻   $   8       C : \ W i n d o w s \ W i n S x s 
\     N´&→∟☺
D   d☺  `☻  ☺   ║q2¾─♥  J   ►♦  ▲♥  ☻   [IY-0  2   d   ♥  ♥   
═Û╬2d
   B   ¿
   ^   @♫  h♥  ♣   D♣(▒¿◄  V    ↕  ÿ♥  ♠   M i c r o s o f t . W i 
n d o w s . S
y s t e m C o m p a t i b l e     l   ☺   ♀☺  ð☺  ☻   ,   ▄☻  
º¹Lí$♦╩☺☺

ETC. ETC......

> Also, I think you should have extern(C) in the function 
> definition.

I based the DLL on this how-to: http://dlang.org/dll.html



More information about the Digitalmars-d-learn mailing list