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