Asked by:
Passing variant from VBScript code to Com DLL Method that accepts Long * as parameter ???

Question
-
How to pass a variant from VBScript code to a COM Method that accepts a pointer to Long as an argument.
Thanks.
Monday, January 31, 2011 10:49 AM
All replies
-
Method Prototype ..
STDMETHODIMP CProcessServerEx::GetProcessIDs(LONG* nProcessIDs, LONG Count)
Call from Script ..
dim myCount()Mgr.GetProcessIDs myCount,3Monday, January 31, 2011 10:51 AM -
Monday, January 31, 2011 2:53 PM
-
k got it ... Thanks ...
But how to pass a Long pointer from C# or vb.net client to a COM DLL Method ...
The client passes a pointer object (Long) that the server methods fills in with ... The number of elements to be filled is also passed on to the client as an in parameter.
IDL CODE:
[id(1), helpstring("method GetProcessIDs")] HRESULT SetIDs([in] LONG Count, [out,size_is(Count)] LONG *myArray);
But the code does not work ..Under C# the intellisense shows the [out,size_is(Count)] LONG *myArray parameter as a out int (A one dimentional varialbe of int/long) .....
Why it is so ?? Even if the IDL has a pointer variable specified ???
I m just started COM/DLL stuffs so a bit ignorant about the internals going on behind the scenes here ......
Any suggestions would be appreciated ......Tuesday, February 1, 2011 7:26 AM -
On 01/02/2011 08:26, Pratik007 wrote:
But how to pass a Long pointer from C# or vb.net client to a COM DLL Method ...
The client passes a pointer object (Long) that the server methods fills in with ... The number of elements to be filled is also passed on to the client as an in parameter.You may want to consider to raise the semantic level of your code.
My understanding is that you are passing a pointer because you actually want to pass an array (and you are passing a pointer to the first item in the array, and the item count as second parameter).
I'd suggest to just use a SAFEARRAY (as Igor already pointed out).Giovanni
Tuesday, February 1, 2011 8:32 AM -
Yes i need to pass a array.
But is there no way to go around this issue except from using a SAFEARRAY ???
Cant it be done using simple pointer stuffs as in C/C++ ????
Tuesday, February 1, 2011 9:05 AM -
Yes i need to pass a array.
But is there no way to go around this issue except from using a SAFEARRAY ???
Cant it be done using simple pointer stuffs as in C/C++ ????
If you want to do it using raw C arrays, you could try like this:
Assuming you have a function (or, mutatis mutandis, COM method) like this from native:
// // Function exported from a native DLL // HRESULT __stdcall NativeDll_GetData(LONG count, LONG * data) { if (data == NULL) return E_POINTER; if (count <= 0) return E_INVALIDARG; // Fill with some data... for (LONG i = 0; i < count; i++) { data[i] = i*10; } return S_OK; }
and you want to use it from C#, you could use a P/Invoke like this:
[DllImport("NativeDll.dll", PreserveSig = false)] private static extern void NativeDll_GetData( int count, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] int[] data);
The PreserveSig=false maps the returning HRESULT (native) to a potential thrown C# exception. SizeParamIndex=0 tells the marshaller that the array size parameter is the first (index 0) one.
This C# test code works fine for me in a simple WinForms app:
private void buttonTest_Click(object sender, EventArgs e) { listBoxData.Items.Clear(); int[] data = new int[5]; NativeDll_GetData(data.Length, data); foreach (var i in data) { listBoxData.Items.Add(i); } }
HTH,
Giovanni
Tuesday, February 1, 2011 9:28 AM -
Yes i need to pass a array.
But is there no way to go around this issue except from using a SAFEARRAY ???
Cant it be done using simple pointer stuffs as in C/C++ ????
You have to use a SAFEARRAY if the client is VBScript. There is no choice. For C#, there are a variety of solutions. Giovanni has provided one.In my view, SAFEARRAY's are actually pretty easy to use, and would work for all COM clients, including VBScript and C#, so you should seriously look at that as the preferred solution.
Tuesday, February 1, 2011 5:39 PM