rednaxela 发表于 2007-11-18 14:56:22

这个时候你需要gdb...有时候觉得在linux上写C程序很郁闷就是一不小心写错了点什么的时候,经常只能看到局Segmentation Fault,什么额外信息都没;Windows上至少还能马上看到crash report...虽然对end-user来说这不算什么好事。D的runtime也是这样,基本上只是给一句Access Violation就没了。这种时候只能靠调试了……
D里能造成Access Violation的东西太多了……例如说这样就会:
void main(char[][] args) {
    char[] message = "Just a simple string.";
    printf("%s", message);
}
原因是message不是null-terminated string,这printf会一直跑下去直到Access Violation出现……
在这个问题上可以用printf("%.*s", message)来解决,不过其它很多细微地方也是就这么给一句错误就没了 T T
至少还想看看call-stack dump之类的信息……

lw 发表于 2007-11-18 15:26:55

貌似是某面试题:于是偶做了其他的答案了…………人家怕是笑死这么EASY的问题

printf("%.*s", message) <<-- 完全不懂,下去试验。。。

lw 发表于 2007-11-18 15:44:26

找到这个:
.*
The precision is not specified in the format string, but as an additional integer value argument
preceding the argument thas has to be formatted.

依然不会:
wchar_t str[] = L"file bytes-copy. ";
fwprintf_s( stdout, L"%.*s: %s(%d) to %s(%d) with length:%d \\n",
    str, argv, offsetsrc, argv, offsetdst, lengthneed );
输出错误的结果:
a.txt: (null)(3617892) to (null)(1000) with length:2600
正确的应该是:
file bytes-copy. : a.txt(0) to b.txt(0) with length:0

shawind 发表于 2007-11-18 17:00:28

还要debug,恩,这个,我去下个gdc和dgdb试试看,故计还是不能成功的可能性较大。

D的字符串数组带来方便的同时,也带来了很多麻烦。
"%.*s"这个怎么说?常见的C教程里好像都没说这个。
wchar_t str[] 这个str还是有结束符号的吧,和D中的不太一样。
我一般都是用toStringz()把D字符串数组转成C风格的char*再用。

rednaxela 发表于 2007-11-18 17:26:02

In D, all dynamic arrays look like a struct containing two fields; a length and a pointer to the data. Thus when you pass 'testString' to printf, what gets put on the stack for printf is the struct. Thus printf gets two values - a length and a pointer.
So its as if you had coded ...
printf("%s\\n", testString.length, testString.ptr);
So it crashes because printf tries to use the length value as if it was a pointer to a C-string.

The correct way to use D-strings with printf is...
printf("%.*s\\n",testString);
The '.*' causes printf to use the next parameter as a length and the subsequent one as the data pointer - which is what you actually passed it.

shawind 发表于 2007-11-18 18:57:32

原来c里面也可以用。只是编译的时候会有警告。

#include <stdio.h>

typedef struct dString
{
    int length;
    char* ptr;
} dString;

int main()
{
    char cs = "test";
    dString ds = {sizeof(cs),&cs};
//    printf("%s \\n",ds);//error
    printf("%.*s \\n",ds);
    return 0;
}


test

shawind 发表于 2007-11-19 20:17:16

问题解决。
做了两个改动。SetCooperativeLevel中的句柄参数,从createwindow获得的。转Dsound.d的时候,把GUID_NULL重写了一下。
之前的错误多半就这两个中的一个,懒得再去追究了。

lw 发表于 2007-11-20 20:05:54

提示,如果注意一下偶上次发的文件就可以发现需要适用GetConsoleWindow的Handle,因为用GetActiveWindows无法得到(根据你的情况推)HWND,所以就失败了,问题在于为什么必须要关联一个窗口,像WAVEOUT WAVEIN就没有必要绑窗口了

另外一种方法是创建一个纯消息窗口,在CREATEWINDOW的时候可以指定……
以上

shawind 发表于 2007-11-20 20:17:04

多半是SetCooperativeLevel的问题吧,com就是麻烦.有了一次经验,就得记住了,句柄这个的东西不是随便用的.

lw 发表于 2007-11-22 22:07:39

个人更加倾向于句柄- -
页: 1 [2]
查看完整版本: [关注]Bindings项目开始完善DX库