P/invoke . why para does not return value ?

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

c++:

extern "C"
__declspec(dllexport)
int myHWGetPhrase(WORD lpInFileName, WORD* lpOutFileName,int sta[2])
{

if (lpInFileName==111)
{
WORD SM[]={1111,2222};
lpOutFileName=SM;
int YM[]={1,2};
sta=YM;

return 21;

}
return 23;
}


----------------------------------------------------------------------------

c#:

[DllImport("HWLX.dll", EntryPoint="myHWGetPhrase", SetLastError=true)]
internal static extern int myHWGetPhrase(ushort lpInFileName, ushort[]
lpOutFileName,int[] sta);

private void button1_Click(object sender, System.EventArgs e) {
ushort[] b=new ushort[2] ;
int[] c=new int[2];
ushort z=111;
int a=myHWGetPhrase(z,b,c);
Console.Write(a.ToString());

}


When it run , a return 21, but b and c , no returned value .
 
You can't assign a value to lpOutFileName and have that 'return' to the
caller. All you're doing is setting the value of what is effectively a
local variable, lpOutFileName. If you want to change the value, you're
going to have to make it a pointer to a pointer and change
*lpOutFileNamePtr.

However, if if you *did* this right, your code wouldn't work. You can't
return a pointer to an array which is stored on the stack of this function!
The contents of the stack will 'go away' as soon as myHWGetPhrase() exits.
The values will probably actually be there for a short time, but they might
disappear at any minute.

Paul T.
 
Back
Top