"SafeArray cannot be marshaled to this array type" error - com

I have a C++ COM local server and C# client. The server code:
// MyStruct as define in the _i.h file
typedef /* [uuid] */ DECLSPEC_UUID("...") struct MyStruct
{
SAFEARRAY * FormatData;
LONG aLong;
BOOL aBool;
} MyStruct;
// Server method being invoked
STDMETHODIMP CMyClass::Foo(MyStruct* StreamInfo, int* result)
{
long Length;
BYTE* Data;
GetData(Length, Data);
PackBytes(Length, Data, &(StreamInfo->FormatData));
}
PackBytes converts the BYTE array to SAFEARRAY. It is taken from this stackoverflow question. It sets the boundary & dimension of the SAFEARRAY.
The client code:
MyStruct myStruct;
int rc = obj.Foo(out myStruct);
Where MyStruct is imported from the COM assembly. it appears as
public struct MyStruct
{
public Array FormatData;
int aLong;
int aBool;
}
After running Foo appears the error "SafeArray cannot be marshaled to this array type because it has either nonzero lower bounds or more than one dimension" with additional remark "Make sure your array has the required number of dimensions".
When debugging the server code it seems Data is properly populated in FormatData: as can be seen in screen-shot below. cElements equals Length and the 18 data pieces are equal to the ones in Data.
Hard-coding Length = 1 did not help. Removing the PackByets call made the error disappear (other fields were passed ok). How can this be fixed?

The PackBytes method that you have referenced constructs a SAFEARRAY with lower bound of 1. Constructing it with a lower bound of zero may fix the problem:
SAFEARRAYBOUND bound{ count, 0 };

Related

C++/CLI pin_ptr's usage in proper way

I am newbie of C++/CLI.
I already know that the pin_ptr's functionality is making GC not to learn to specified object.
now let me show you msdn's example.
https://msdn.microsoft.com/en-us//library/1dz8byfh.aspx
// pin_ptr_1.cpp
// compile with: /clr
using namespace System;
#define SIZE 10
#pragma unmanaged
// native function that initializes an array
void native_function(int* p) {
for(int i = 0 ; i < 10 ; i++)
p[i] = i;
}
#pragma managed
public ref class A {
private:
array<int>^ arr; // CLR integer array
public:
A() {
arr = gcnew array<int>(SIZE);
}
void load() {
pin_ptr<int> p = &arr[0]; // pin pointer to first element in arr
int* np = p; // pointer to the first element in arr
native_function(np); // pass pointer to native function
}
int sum() {
int total = 0;
for (int i = 0 ; i < SIZE ; i++)
total += arr[i];
return total;
}
};
int main() {
A^ a = gcnew A;
a->load(); // initialize managed array using the native function
Console::WriteLine(a->sum());
}
hear is the question.
Isn't it okay, the passed object(arr) not pinned ?
because the unmanaged code(native_function) is sync operation and finished before the C++/CLI code (load)
is there any chance the gc destory arr, even though the main logic is running?
(I think A is main's stack variable and arr is A's member variable, so while running main, it should visible)
if so, how can we guarantee that the A is there before invoking load?
(only while not running in native-code?)
int main() {
A^ a = gcnew A;
// I Think A or arr can be destroyed in here, if it is able to be destroyed in native_function.
a->load();
...
}
Thanks, in advance.
The problem that is solved by pinning a pointer is not a normal concurrency issue. It might be true that no other thread will preempt the execution of your native function. However, you have to count in the garbage collector, which might kick in whenever the .NET runtime sees fit. For instance, the system might run low on memory, so the runtime decides to collect disposed objects. This might happen while your native function executes, and the garbage collector might relocate the array it is using, so the pointer you passed in isn't valid anymore.
The golden rule of thumb is to pin ALL array pointers and ALL string pointers before passing them to a native function. ALWAYS. Don't think about it, just do it as a rule. Your code might work fine for a long time without pinning, but one day bad luck will strike you just when it's most annoying.

What is this Objective C code doing

I am a developer in C-like languages (Java/JavaScript/C#) and I am attempting to convert some Objective-C code into Java.
For the most part, it is relatively straightforward but I have hit a stumbling block with the following bit of code:
typedef struct {
char *PAGE_AREA_ONE;
char *PAGE_AREA_TWO;
char *PAGE_AREA_THREE;
} CODES;
- (CODES*) getOpCode {
CODES *result = NULL;
result = malloc(sizeof(CODES));
result->PAGE_AREA_ONE = "\x1b\x1b\x1b";
result->PAGE_AREA_TWO = "\x2d\x2d\x2d";
result->PAGE_AREA_THREE = "\x40\x40";
return result;
}
What would the Java equivalent of this be? From what I can tell in other areas of the code, it is being used to store constants. But I am not 100% certain.
Thanks.
The typedef is just creating a structure that contains three string properties. The getOpCode method is apparently trying to create a new structure and assign values to those three properties. C# code would be:
public class Codes
{
public string PageAreaOne;
public string PageAreaTwo;
public string PageAreaThree;
}
public Codes GetCodes()
{
Codes result = new Codes();
result.PageAreaOne = "\x1b\x1b\x1b"; // three ESC characters
result.PageAreaTwo = "---";
result.PageAreaThree = "##";
return result;
}
The code in question is allocating a block of memory that the size of the CODES structure, filling it with some data, and returning a pointer to the new block. The data is apparently some operation codes (that is, instructions) for something, so perhaps the data is being sent to some other device where the instructions will be executed.

Use of THIS-> in C/C++ [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm going through a tutorial on audio for the iphone and it uses C/C++. I'm not familiar with the use of THIS->. It seems to refer to a pointer to global variable. Here is the tutorial - iPhone Core Audio Part 3 – Audio Callback.
The statement I am trying to understand is the THIS-> part of the statement:
// Pass in a reference to the phase value, you have to keep track of this
// so that the sin resumes right where the last call left off
float phase = THIS->sinPhase;
The tutorial indicate that THIS-> is used to get a to access AudioController variables. It seems that sinPhase is global variable.
Please explain why "phase" reference is created instead of just referring directly to the global variable "sinPhase". Keep in mind I am an objective C programming trying to understand this C/C++ code.
In this example, THIS is not a reference to a global variable; it is defined above in the function, as a cast of the void pointer inRefCon:
static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{
// Get a reference to the object that was passed with the callback
// In this case, the AudioController passed itself so
// that you can access its data.
AudioController *THIS = (AudioController*)inRefCon;
This is a fairly common pattern in C; in order to pass a callback in to some API, so that it can later call your code, you pass both a function pointer and a void pointer. The void pointer contains whatever data your function pointer will need to operate on. Within your callback, you will need to cast it back to a pointer to the actual type, so you can access the data within it. In this case, the author of the example is naming that cast pointer THIS, probably to make this look more object-oriented, even though this is just C and THIS has no special meaning.
You ask why they assign it to a local variable rather than just using THIS->sinPhase everywhere. There's no reason you couldn't use THIS->sinPhase everywhere; they likely just assigned it to a local variable phase to save on typing. There's a small chance that the optimizer could do a better job on a local variable than on one passed in via a pointer, because it can make more assumptions about the local variable (in particular, it can assume that no one else is updating it at the same time). So the loop might run slightly faster using a local variable, though I wouldn't be certain without testing; the most likely reason is just to save typing and make the code more readable.
Here's a simplified example of how a callback API like this works; hopefully this should make it easier to understand how a callback API works, without trying to understand the rest of what's going on in Core Audio at the same time. Let's say I want to write a function that will apply a callback to an integer 10 times. I might write:
int do_ten_times(int input, int (*callback)(int)) {
int value = input;
for (int i = 0; i < 10; ++i) {
value = callback(value);
}
return value;
}
Now I could call this with different functions, like the following add_one() or times_two():
int add_one(int x) {
return x + 1;
}
int times_two(int x) {
return x * 2;
}
result = do_ten_times(1, add_one);
result = do_ten_times(1, times_two);
But say I want to be able to add or multiply by different numbers; I could try writing one function for each number that you wanted to add or multiply by, but then you would run into a problem if the number wasn't fixed in the code, but was based on input. You can't write one function for each possible number; you are going to need to pass a value in. So let's add a value to our callbacks, and have do_ten_times() pass that value in:
int do_ten_times(int input, int (*callback)(int, int), int data) {
int value = input;
for (int i = 0; i < 10; ++i) {
value = callback(value, data);
}
return value;
}
int add(int x, int increment) {
return x + increment;
}
int times(int x, int multiplier) {
return x * multiplier;
}
result = do_ten_times(1, add, 3);
result = do_ten_times(1, times, 4);
But what if someone wants to write a function that varies by something other than an integer? For instance, what if you want to write a function that will add different numbers depending on whether the input is negative or positive? Now we need to pass two values in. Again, we could extend our interface to pass in two values; but we will eventually need to pass in more values, values of different types, and the like. We notice that do_ten_times really doesn't care about the type of the value we're passing in; it just needs to pass it to the callback, and the callback can interpret it however it likes. We can achieve this with a void pointer; the callback then casts that void pointer to the appropriate type to get the value out:
int do_ten_times(int input, int (*callback)(int, void *), void *data) {
int value = input;
for (int i = 0; i < 10; ++i) {
value = callback(value, data);
}
return value;
}
int add(int x, void *data) {
int increment = *(int *)data;
return x + increment;
}
int times(int x, void *data) {
int multiplier = *(int *)data;
return x * multiplier;
}
struct pos_neg {
int pos;
int neg;
};
int add_pos_neg(int x, void *data) {
struct pos_neg *increments = (struct pos_neg *)data;
if (x >= 0)
return x + increments->pos;
else
return x + increments->neg;
}
int i = 3;
result = do_ten_times(1, add, &i);
int m = 4;
result = do_ten_times(1, times, &m);
struct pos_neg pn = { 2, -2 };
result = do_ten_times(-1, add_pos_neg, &pn);
These are all, of course, toy examples. In the Core Audio case, the callback is used to generate a buffer of audio data; it is called every time the audio system needs to generate more data in order to keep playing smoothly. The information passed via the void *inRefCon is used to track how exactly where in the sine wave you have gotten to in the current buffer, so the next buffer can pick up where the last one left off.
If it's not declared in scope (i.e. local variable declared in that context), I see two options:
either it's a define, and it actually refers to the instance: #define THIS this.
either a global variable, as you suggested.
THIS doesn't have any inherent meaning in C++, so it can be either. Whichever it is though, it's pretty awful.
The reason it's copied in a different variable, in case it's a global, and not used directly, can be either for clarity or to not accidentally modify it.

How to Calculate CheckSum in objective C

I am working on an app that deals with external hardware communication, and I'm having problem getting checksum of a package. (I'm still learning objective C while making this app, so I'm still fairly new in this.)
In another code written in C, the checksum was calculated like this:
byte CalculateCheckSum (byte txcount){
byte local_cs=0;
while(txcount>0){
local_cs+=*x_ptr;
x_ptr += 1;
txcount--;
};
return (~local_cs+1);
}
I tried to use the some code for objective C:
u_int8_t synByteSOH[]= {SYN,SYN,SOH,SETSERIALINFO};
- (Byte)CalcCheckSum:(Byte)i
{ u_int8_t synByteSOH[]= {SYN,SYN,SOH,SETSERIALINFO};
Byte local_cs = 0;
int j = 0;
while (i>0) {
local_cs += synByteSOH[j];
i--;
j++;
};
return (~local_cs+1);
}
No warnings or errors, but it's said clang: error: linker command failed with exit code 1
Does anyone know why is that? And how should I fix it?
All valid C code it valid Objective C code. Don't try rewriting it into a Objective C method - there's zero value to that.
The trick to adapting the original function is that it relies on several things: byte being typedeffed, and a global/static variable called x_ptr.
About x_ptr, where it comes from and how is it initialized in the original, we know nothing from the pasted snippet. So assuming that the byte array synByteSOH is the data block you need a checksum of, just introduce a file static variable x_ptr of type byte* and initialize it to your data block:
typedef unsigned char byte;
static byte *x_ptr;
byte CalculateCheckSum (byte txcount)
//...Follows as pasted
//...
//And now we call it elsewhere:
byte synByteSOH[]= {SYN,SYN,SOH,SETSERIALINFO};
x_ptr = synByteSOH
byte Checksum = CalculateCheckSum(4); //4 is the data block size
In general, even in an ObjC project it's perfectly OK to have C functions. No one ever said every piece of code should be in a class method.

How to Marcshal a COM callback in C++/CLI

We have an out of process COM application that was written in C++ that implements are networking protocol. When a packet of data is received a callback is invoked into the application that registered for the packet.
The callback interface is defined as such:
[helpstring("method MessageHandler")] HRESULT MessageHandler([in,size_is(nSize)] char * szBuf, int nSize, DWORD dwTransCode, DWORD dwSenderID, BSTR bstrFromIP);
Using this in C++ has not been an issue. We now have a case where we have a C++/CLI application that needs to receive callbacks. After hacking away until the compiler was happy, I arrived at the following implementation:
ref class MessageHandlerClass : public MessageRouterCallback
{
public:
virtual void MessageHandler(signed char %buffer, int size, unsigned int msgId, unsigned int fromId, System::String^ fromIp)
{
switch (msgId)
{
case MaintenanceMsgs::maintenance_event_message::ID:
{
SomeStructure msg;
myHandler->HandleMaintenanceEvent(&msg);
}
}
}
This is my first foray into C++/CLI.
First Question: What they heck does '%' mean in 'signed char %buffer'?
Second Question: I can place a breakpoint and see that the callback is getting called. I can look at the 'buffer' argument in the memory debugger and it contains the data I expect. I have been VERY unsuccessful at pulling that data out and placing it into the variable 'msg'. I know I can't do a cast like in C++, but every example I've been tried (Mostly InteropServices::Marshal and some pin_ptr stuff) doesn't seem to get me anywhere.
SomeStructure is declared in a header file and is included by both the C++ and the C++/CLI application. SomeStructure contains 2 unsigned shorts followed by three character arrays.
Any direction on where to go from here would be greatly appreciated.
Ok. It just 'clicked'... So I'll answer my own question here.
First Question: What they heck does '%' mean in 'signed char %buffer'?
The '%' just means its 'tracked' and will be garbage collected, or at least that's what I think it means :)
Second Question: How to marshal.
First I had to get to the 'internal pointer' and C++/CLI provides the & operator for that. Then I was able to simply memcpy the data over.
pin_ptr<signed char> p = &buffer;
MaintenanceMsgs::maintenance_event_message msg;
memcpy((void*)&msg, (void*)p, sizeof(msg));
myHandler->HandleMaintenanceEvent(&msg);
Is this safe?