方法参数有问题.... [英] problem with method parameters....
问题描述
我正在调用一个API方法,该方法需要传递引用数组作为参数.问题在于该参数
实际的功能是
BOOL __stdcall G3BlockScan(int hRadio, DWORD * Freqs ,int Count,int StopSquelchRaw,DWORD FeedbackTime,HWND WinHandle,DWORD Msg);
此处的Freqs是一个数组,其内容由API函数调用进行了修饰.
我将此函数包装在C#中,如下所示..
[DllImport("wrg303api.dll")]
公共静态外部布尔G3BlockScan(int hRadio, uint [] Freqs ,int Count,int StopSquelchRaw,uint FeedbackTime,IntPtr WinHandle,uint Msg);
和该函数呼叫就像这样.....
.....一些代码.....
uint []频率=新uint [1001]; //扫描频率的缓冲区
.....一些代码....
if(!clsApiWrapper.G3BlockScan(radioHandle, Freqs ,1001,256, 100000,指针,消息))
MessageBox.Show(块扫描无法启动", "WiNRADiO-错误消息",MessageBoxButtons.OK,MessageBoxIcon.Error);
.....一些代码....
但现在,当我运行该应用程序时,它不会更改数组的内容.所以我相信我需要将数组的引用传递给函数.如果我更改这样的代码以传递数组的引用
[DllImport( "wrg303api.dll")]
.....一些代码.....
uint [] Freqs = new uint [1001]; //扫描频率的缓冲区
.....一些代码....
if(!clsApiWrapper.G3BlockScan(radioHandle, ref Freqs ,1001,256 ,100000,pointer,Msg))
MessageBox.Show(块扫描无法启动" ,"WiNRADiO-错误消息",MessageBoxButtons.OK,MessageBoxIcon.Error);
现在,当我访问方法调用后的Freqs数组遇到一个奇怪的行为..Freqs数组的大小从1000减少到1.
我不知道出了什么问题.
any1 plz可以建议什么吗?
I a calling an API method which requires passing the reference to an array as parameter. The problem is with that parameter
The actual function is BOOL __stdcall G3BlockScan(int hRadio,DWORD *Freqs, int Count,int StopSquelchRaw, DWORD FeedbackTime,HWND WinHandle,DWORD Msg);
Here Freqs is an array whose contents are mofied by the The API function call.
I have wrapped this function in C# as follows..[DllImport("wrg303api.dll")]
public static extern bool G3BlockScan(int hRadio, uint [] Freqs, int Count,int StopSquelchRaw,uint FeedbackTime,IntPtr WinHandle,uint Msg);
and the function call is like this .......... some code.....
uint[] Freqs = new uint[1001]; // buffer for frequencies to scan
..... some code....
if (!clsApiWrapper.G3BlockScan(radioHandle, Freqs, 1001, 256, 100000, pointer,Msg))
MessageBox.Show("The block scanning failed to start", "WiNRADiO - Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
..... some code ....
But now when I run the app it doesn't change the contents of the array. so I believe I need to pass the reference of the array to the function. And if I change the code like this to pass the reference of the array[DllImport("wrg303api.dll")]
public static extern bool G3BlockScan(int hRadio, ref uint [] Freqs, int Count,int StopSquelchRaw,uint FeedbackTime,IntPtr WinHandle,uint Msg);
..... some code.....
uint[] Freqs = new uint[1001]; // buffer for frequencies to scan
..... some code....
if (!clsApiWrapper.G3BlockScan(radioHandle,ref Freqs, 1001, 256, 100000, pointer,Msg))
MessageBox.Show("The block scanning failed to start", "WiNRADiO - Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
Now when I access the Freqs array after the method call I encounter a strange behaviour.. The size of Freqs array is reduced from 1000 to 1.
I can't figure out what is going wrong.
Can any1 plz suggest what to do.
推荐答案
您是否忘了参数的'out'语句?
Didn't you forget the 'out' statement for the parameter?
[DllImport("wrg303api.dll")]
公共静态外部布尔G3BlockScan(int hRadio,
[DllImport("wrg303api.dll")]
public static extern bool G3BlockScan(int hRadio, out uint [] Freqs, int Count,int StopSquelchRaw,uint FeedbackTime,IntPtr WinHandle,uint Msg);
如果我错了,请纠正我;)
correct me if i'm wrong ;)
这篇关于方法参数有问题....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!