Call C function - Access violation

Gary Willoughby via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 3 05:23:25 PST 2016


On Sunday, 3 January 2016 at 12:30:34 UTC, TheDGuy wrote:
> I get an access violation with this code:
>
> ...

There are a few things you can do to improve your code to make it 
easier to debug.

1. When converting a D string to a char pointer for use with C, 
use `std.string.toStringz`:

http://dlang.org/phobos/std_string.html#.toStringz

2. When converting a char pointer to a D string use 
`std.conv.to!(string)`:

http://dlang.org/phobos/std_conv.html#.to

3. Define C-style char pointers in D as `const(char)*`.
4. Don't use the variable name `text` as it conflicts with 
`std.conv.text`.


I think I've noticed one problem with the code above. You are 
using `text.ptr`. You shouldn't do that because you are passing a 
pointer not an array. Just use `text`.


More information about the Digitalmars-d-learn mailing list