I have a .DLL file in C. The primary structure required by all functions in that DLL is of the following form.
typedef struct
{
char *snsAccessID;
char *snsSecretKey;
char *snsPath;
char *snsTopicName;
char *snsTopicAmazonResourceName;
char *snsDisplayName;
char *snsOwnerId;
} snsTopic, *HSNS;
One of the functions just for example is as follows:
BOOL SnsOpenTopic(char *accessID, char *secretKey, char *ownerId, char *path, char *topicName, char *displayName, HSNS *snsTopicHandle);
All char pointers above are Input parameters.
I am using C# with .NET CF 3.5 on a WinCE6/7 device.
I have tried using a class and then passing the pointer to the structure required by the C function as follows:
public class HSNS
{
public string snsAccessID;
public string snsSecretKey;
public string snsPath;
public string snsTopicName;
public string snsTopicAmazonResourceName;
public string snsDisplayName;
public string snsOwnerId;
}
[DllImport("Cloud.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SnsOpenTopic(string accessID, string secretKey, string ownerId, string path, string topicName, string displayName, ref HSNS snsTopicHandle);
Using the C# snippet above results in NotSupportedException being thrown. I can't figure out what's wrong with the above C# code?
Another thing i tried is using unmanaged code with C#.
unsafe public struct HSNS
{
public char *snsAccessID;
public char *snsSecretKey;
public char *snsPath;
public char *snsTopicName;
public char *snsTopicAmazonResourceName;
public char *snsDisplayName;
public char *snsOwnerId;
}
[DllImport("Cloud.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SnsOpenTopic(string accessID, string secretKey, string ownerId, string path, string topicName, string displayName, HSNS *snsTopicHandle);
fixed (HSNS *snsAcsTopicHandle = &snsAcsTopic)
{
if (SnsOpenTopic(AWS_ACCOUNT_ACCESS_ID, AWS_ACCOUNT_SECRET_KEY, AWS_ACCOUNT_OWNER_ID, AWS_SNS_SINGAPORE_REGION, topicName, displayName, snsAcsTopicHandle))
{
}
}
In the above case, in debug i can check the pointers inside the structure are not being populated and in the debug view i can see Invalid Reference. Cannot dereference pointer message. The rest of the functions fail because of this.
What is the correct way to use Platform Invoke and Marshalling for the above scenario. I have tried searching on Google and Stack overflow. Didn't find a use case similar to mine.