1
するちんぷんかんぷん、私は以下のようなものを持っているCライブラリを持っている:D出力
Abc.hは今 void test_string(Abc* abcobj, char* strA, char* strB)
{
printf("Str A is %s and str B is %s\n", strA, strB);
return;
}
Abc* create_abc()
{
Abc* abcobj;
abcobj = (Abc*) calloc(1, sizeof(Abc));
return abcobj;
}
void test_string(Abc* abcobj, char* strA, char* strB)
{
printf("Str A is %s and str B is %s\n", strA, strB);
return;
}
Abc* create_abc()
{
Abc* abcobj;
abcobj = (Abc*) calloc(1, sizeof(Abc));
return abcobj;
}
Abc.c
typedef struct
{
uint8_t dbtype;
uint32_t dbcount;
} Abc;
void test_string(Abc* abcobj, char* strA, char* strB);
Abc* create_abc(void);
を提出します私は自分のDコードでこれらの関数を呼び出そうとしています。
testD.d
module testD;
import std.stdio;
import core.stdc.string;
import core.sys.posix.dlfcn;
extern (C)
{
struct Abc {
ubyte dbtype;
int dbcount;
}
}
int main() {
auto lib = dlopen("Abc.so".ptr, RTLD_LAZY | RTLD_LOCAL);
if (lib is null) {
return -1;
}
void function(Abc* abcobj, char* strA, char* strB) test_string = cast(void function(Abc* abcobj, char* strA, char* strB))dlsym(lib, "test_string");
Abc* function() create_abc = cast(Abc* function())dlsym(lib, "create_abc");
char* strA = cast(char*)("this is string one");
char* strB = cast(char*)("this is string two");
Abc* abcobj = create_abc();
test_string(abcobj, strA, strB);
if (dlclose(lib) == 0) {
return 0;
}
else {
return -1;
}
} // main() function
私が使用してtestD.dをコンパイル:
testD.d DMD
test_stringが文を出力するとその後./testD
を実行しますstrBの値は常にぎこちなくなり、strBはうまく出ます。
なぜこれが起こっていますか?
他のもの: 'dbcount'はCの' uint32_t'にマッチするDの 'uint'でなければなりません。' test_string'は文字列を変更する必要がないので 'const char *'を受け入れるべきです'immutable(char)[]'〜 'char *'型の文字列リテラルは、技術的には未定義の動作です)。 –
タイプをuintに変更し、const char *を使用しました。 キャスティングパートがchar * strA = "this is string one" .dup.ptrに変更されました。 – Vlam