Question

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.

Was it helpful?

Solution

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top