문제

This is my C++ unmanaged dll code.

void Only(char *a)
{
    *a++ = 'p';
    *a++ = 'r';
    *a++ = 'a';
    *a++ = 'n';
    *a++ = 'i';
    *a++ = 0;
}

.def file is used for exporting symbols.

I have used Only function in VB.Net application as follows,

Declare Sub Only Lib "dllproj2.dll" Alias "Only" (b As StringBuilder)
....
Dim s As StringBuilder = New StringBuilder()
Only(s)

It works fine. But when I try to use b as ByRef as follows,

Declare Sub Only Lib "dllproj2.dll" Alias "Only" (ByRef b As StringBuilder)

It gives exception shown in image.

enter image description here

I do not understand why. Doesn't ByRef is more appropriate here?

Thanks.

도움이 되었습니까?

해결책

It must be ByVal. The function will not overwrite the reference to StringBuilder itself. It will rather update the content of the StringBuilder. (See here for a similar question.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top