AMD Gpu Memory dll - c#

I'm writing an C++ dll for using in a C# application.
The dll will check the total GPU memory and the usage of the GPU memory.
Now I have created three methods. The first one does initialize GLew and other OpeGl stuff. The second will read the total memory of the GPU. And the last one will read the GPU usage.
The inialize and the total memory methods does work but with the last one I get some problems. When I call the methode it stops and when I debug it I can set a breakpoint on the delete[] ids; line without any problem. But it does not return anythin on the return available line (it does not get there). When I remove the delte[] ids line I get an error:
'Run-Time Check Failure #2 - Stack around the variable 'nCurAvailMemoryInKB' was corrupted.'. Do I something wrong to read the usage of the GPU memory?
__declspec(dllexport) float getAvailableMemory()
{
int available = -1;
if (wglGetGPUIDsAMD && wglGetGPUInfoAMD)
{
UINT n = wglGetGPUIDsAMD(0, 0);
UINT * ids = new UINT[n];
wglGetGPUIDsAMD(n, ids);
GLint nCurAvailMemoryInKB = 0;
glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI,
&nCurAvailMemoryInKB);
available = nCurAvailMemoryInKB;
delete[] ids;
}
return available;
}
I created a test caller for the Dll in C#:
class Program {
[DllImport("AmdLib.dll")]
public static extern bool init();
[DllImport("AmdLib.dll")]
public static extern int getTotalMemory();
[DllImport("AmdLib.dll")]
public static extern float getAvailableMemory();
static void Main(string[] args) {
init();
Console.WriteLine("Total");
Console.WriteLine(getTotalMemory());
Console.WriteLine("Available");
Console.WriteLine(getAvailableMemory());
}
}
And the full C++ DLL source does looks like:
#include <stdio.h>
#include <windows.h>
#include <GL/glew.h>
#include <GL/wglew.h>
#include <assert.h>
#include <vector>
#include <string>
using namespace std;
extern "C"
{
static HGLRC ctx = NULL;
__declspec(dllexport) bool init()
{
HWND hwnd = NULL;
HINSTANCE hinstance = (HINSTANCE)GetModuleHandle(NULL);
WNDCLASSA window_class;
window_class.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_GLOBALCLASS;
window_class.lpfnWndProc = DefWindowProc;
window_class.cbClsExtra = 0;
window_class.cbWndExtra = 0;
window_class.hInstance = hinstance;
window_class.hIcon = NULL;
window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
window_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
window_class.lpszMenuName = NULL;
window_class.lpszClassName = "test_class";
ATOM atom = RegisterClassA(&window_class);
hwnd = CreateWindowA("test_class", "htest", WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 1, 1, 1, 1, NULL, NULL, hinstance, NULL);
if (hwnd == NULL) {
DWORD err = GetLastError();
return false;
}
HDC hDC = GetDC(hwnd);
if (hDC == NULL) {
return false;
}
PIXELFORMATDESCRIPTOR const pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA,
0,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
0,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pixel_format = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pixel_format, &pfd);
ctx = wglCreateContext(hDC);
if (ctx) {
if (!wglMakeCurrent(hDC, ctx)) {
return false;
}
}
ReleaseDC(hwnd, hDC);
GLenum glew = glewInit();
return true;
}
static void check_gl_error()
{
GLenum error = glGetError();
assert(error == GL_NO_ERROR);
}
__declspec(dllexport) int getTotalMemory()
{
if (wglGetGPUIDsAMD && wglGetGPUInfoAMD)
{
UINT n = wglGetGPUIDsAMD(0, 0);
UINT * ids = new UINT[n];
UINT total_mem_mb = 0;
wglGetGPUIDsAMD(n, ids);
wglGetGPUInfoAMD(ids[0], WGL_GPU_RAM_AMD, GL_UNSIGNED_INT, sizeof(UINT), &total_mem_mb);
delete[] ids;
return total_mem_mb;
}
return -1;
}
__declspec(dllexport) float getAvailableMemory()
{
int available = -1;
if (wglGetGPUIDsAMD && wglGetGPUInfoAMD)
{
UINT n = wglGetGPUIDsAMD(0, 0);
UINT * ids = new UINT[n];
wglGetGPUIDsAMD(n, ids);
GLint nCurAvailMemoryInKB = 0;
glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI,
&nCurAvailMemoryInKB);
available = nCurAvailMemoryInKB;
//delete[] ids;
}
return available;
}
}

Since I don't have an ATI card to test with, off the top of my head I'd guess the first wglGetGPUIDsAMD call returns 0, you allocate a 0-length array (which works) and at the end you try to delete it (which throws). Somewhere in-between you overwrite the memory around that pointer with data (thus corrupting the guards and making VS throw).
Now looking at what you're actually doing with that array, or the knowledge of how many GPUs you have, you never actually use either of them. You can literally delete both calls to wglGetGPUIDsAMD and the array allocation/deallocation, and just call glGetIntegerv.

Related

C++ & C# Call CryptEncrypt and CryptDecrypt

Hello i am trying to Encrypt and Decrypt between C++ and C# have made a dll project using CryptEncrypt wincrypt here code i used
C++
#include <Windows.h>
#include <stdio.h>
extern "C" __declspec(dllexport)
BOOL EncryptData(char* szData, char* szPassword, char* szErroror, BYTE* pData, BYTE* pDataLen, BOOL Encrypt)
{
HANDLE hSourceFile = INVALID_HANDLE_VALUE;
HANDLE hDestinationFile = INVALID_HANDLE_VALUE;
HCRYPTPROV hProv = NULL;
HCRYPTKEY hKey = NULL;
HCRYPTKEY hXchgKey = NULL;
HCRYPTHASH hHash = NULL;
PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;
PBYTE pbBuffer = NULL;
DWORD dwBlockLen = 0;
DWORD dwBufferLen = 0;
DWORD dwCount = 0;
bool bRet = true;
int len = strlen(szData);
if (!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
if (!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
return false;
}
// Create a hash object.
if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
return false;
// Hash in the password data.
if (!CryptHashData(hHash, (BYTE*)szPassword, strlen(szPassword), 0))
return false;
// Derive a session key from the hash object.
if (!CryptDeriveKey(hProv, CALG_RC4, hHash, 0x00800000, &hKey))
return false;
dwBlockLen = 1000 - 1000 % 8;
// Allocate memory.
if ((pbBuffer = (BYTE*)malloc(dwBufferLen)) == NULL)
return false;
if (Encrypt)
{
if (!CryptEncrypt(hKey, 0, false, 0, pbBuffer, &dwCount, dwBufferLen))
{
DWORD dwError = GetLastError();
strcpy(szErroror, "CryptEncrypt Failed Error ");
char szError[10];
memset(szError, 0, 10);
sprintf(szError, "%d", dwError);
strcat(szErroror, szError);
bRet = false;
}
}
else
{
if (!CryptDecrypt(hKey, 0, false, 0, pbBuffer, &dwCount))
{
DWORD dwError = GetLastError();
strcpy(szErroror, "CryptDecrypt Failed Error ");
char szError[10];
memset(szError, 0, 10);
sprintf(szError, "%d", dwError);
strcat(szErroror, szError);
bRet = false;
}
}
char szDataLen[16];
memset(szDataLen, 0, 16);
sprintf(szDataLen, "%d", dwCount);
memcpy(pDataLen, szDataLen, 16);
BYTE* pMyData = (BYTE*)malloc(len);
memset(pMyData, 0, len);
memcpy(pData, pbBuffer, len);
// Free memory.
if (pbKeyBlob) free(pbKeyBlob);
if (pbBuffer) free(pbBuffer);
if (pMyData) free(pMyData);
// Destroy session key.
if (hKey) CryptDestroyKey(hKey);
// Release key exchange key handle.
if (hXchgKey) CryptDestroyKey(hXchgKey);
// Destroy hash object.
if (hHash) CryptDestroyHash(hHash);
// Release provider handle.
if (hProv) CryptReleaseContext(hProv, 0);
return bRet;
}
And C# Project Calling the dll and the funcation
namespace ConsoleApp1
{
class Program
{
[DllImport("Project3.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EncryptData(byte[] szData, char[] szPassword, StringBuilder sbError, byte[] pData, byte[] pDataLen, bool Encrypt);
static void Main(string[] args)
{
StringBuilder sbError = new StringBuilder(255);
byte[] szData = Encoding.ASCII.GetBytes("datatest");
char[] szPassword = ("test").ToCharArray();
byte[] pData = new byte[1008];
byte[] pDataLen = new byte[16];
Console.WriteLine("Encrypt");
bool bRet = EncryptData(szData, szPassword, sbError, pData, pDataLen, true);
Console.WriteLine(Encoding.ASCII.GetString(pData));
Console.WriteLine("Decrypt");
bool bRet2 = EncryptData(szData, szPassword, sbError, pData, pDataLen, false);
Console.WriteLine(Encoding.ASCII.GetString(pData));
Console.ReadKey();
}
}
}
but i have a problem with Decrypt
out
Encrypt ??r c?
Decrypt datatestx?
Encrypt 1?8V?6N5l???
Decrypt
d ?? -
as you can see the Decrypt is wrong what could be wrong
First of all your C/C++ code uses deprecated Windows functions, secondly, there are a lot of "unsecure" C runtime functions used like: "strcpy", "strcat", "sprintf" etc. etc., thirdly not all code is guarding against buffer coding errors. The C code as is does not compile in any recent Visual Studio compiler without significant corrections.
Pls try fixing all errors, simplify code and post it again.
On the C# side, data buffers that are passed to C code are not pinned and can be moved by GC at any time. Due to its small size, they could be eagerly promoted from Gen 0 to Gen 1 by GC what will cause memory move operation and invalidation of the pointer to buffer. To put it simply pointers to all buffers may be invalid at the moment they are read by C code.
On the C# side you could do the following:
[DllImport("Project3.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.Bool)]
public static unsafe extern bool EncryptData(
byte* szData, char* szPassword, StringBuilder sbError, byte* pData, byte* pDataLen, bool Encrypt);
static unsafe void Main(string[] args)
{
StringBuilder sbError = new StringBuilder(255);
byte[] szDataBuff = Encoding.ASCII.GetBytes("datatest");
char[] szPasswordBuff = ("test").ToCharArray();
byte[] pDataBuff = new byte[1008];
byte[] pDataLenBuff = new byte[16];
fixed (byte* szData = szDataBuff)
fixed (char* szPassword = szPasswordBuff)
fixed (byte* pData = pDataBuff)
fixed (byte* pDataLen = pDataLenBuff)
{
Console.WriteLine("Encrypt");
bool bRet = EncryptData(szData, szPassword, sbError, pData, pDataLen, true);
Console.WriteLine("Encrypted: {0}", bRet);
Console.WriteLine(Marshal.PtrToStringAnsi((IntPtr)pData));
Console.WriteLine("Decrypt");
bool bRet2 = EncryptData(szData, szPassword, sbError, pData, pDataLen, false);
Console.WriteLine("Derypted: {0}", bRet2);
Console.WriteLine(Marshal.PtrToStringAnsi((IntPtr)pData));
}
Console.ReadKey();
}
There are other approaches possible but this one seems to be most straightforward and simple.

Unable to pass System.Drawing.Bitmap to C++ DLL

I'm writing a DLL in C++ to search a subpicture in a Bitmap.
When the C++ Part is executed, the HBITMAP is not valid.
Here is my code:
C#:
[System.Runtime.InteropServices.DllImport("FindSubPicture.dll", EntryPoint = "FindSubPictures", CallingConvention = CallingConvention.Cdecl)]
private static extern System.IntPtr FindSubPicturesFct(System.IntPtr mImg, System.IntPtr sImg, int* nMatches);
public static System.Collections.Generic.List<TPoint> FindSubPictures(System.Drawing.Bitmap mImg, System.Drawing.Bitmap sImg)
{
TPoint* PStack = null;
int nMatches = 0;
System.Collections.Generic.List<TPoint> MyList = new System.Collections.Generic.List<TPoint>();
MyList.Clear();
PStack = (TPoint*)FindSubPicturesFct(mImg.GetHbitmap(), sImg.GetHbitmap(), &nMatches);
if(PStack == null) { return MyList;}
for (int i = 0; i < nMatches; i++) { MyList.Add(new TPoint(PStack[i].x[0], PStack[i].x[1])); }
try
{
System.Runtime.InteropServices.Marshal.FreeHGlobal((System.IntPtr)PStack);
}catch(System.Exception ex) { System.Console.WriteLine(ex.Message); }
return MyList;
}
C++:
#include "FindSubPictures.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <omp.h>
struct TPoint
{
int x[2];
TPoint(int fx, int fy) {
x[0] = fx;
x[1] = fy;
}
void Reset() { x[0] = 0; x[1] = 0; }
void Set(int fx, int fy) { x[0] = fx; x[1] = fy; }
};
extern "C" __declspec(dllexport) TPoint* FindSubPictures(HBITMAP mImg, HBITMAP sImg, int* nMatches) {
int mImgWidth = -1, mImgHeight = -1, sImgWidth = -1, sImgHeight = -1;
TPoint* MyList = nullptr;
if (mImg == nullptr || sImg == nullptr || nMatches == nullptr) { return nullptr; }
return MyList;
}
Well, the C++ Library function is not doing anything until now. That's because the HBITMAP is not valid. In C#, the System.Drawing.Bitmap is valid.
When I enter "mIng.", no auto-completion is available.
Am I missing anything?
Obtaining a bitmap does not mean you can access it like a data field. You'll have to lock the bitmap data in memory first.
See also the answers in this thread here.
Bitmap.LockBits
Code would look as follows:
Rectangle rect = new Rectangle(0, 0, mImg.Width, mImg.Height);
System.Drawing.Imaging.BitmapData bmpData =
mImg.LockBits(rect, System.Drawing.Imaging.ImageLockMode.Read,
mImg.PixelFormat);
// Get the address of the first line.
IntPtr ptr2mImg = bmpData.Scan0;
Ok, I have the solution.
HBITMAP is only a handle "void *"
You first have to get the complete object by the function GetObject
Code:
extern "C" __declspec(dllexport) TPoint* FindSubPictures(HBITMAP mImg, HBITMAP sImg, int* nMatches) {
int mImgWidth = -1, mImgHeight = -1, sImgWidth = -1, sImgHeight = -1;
TPoint* MyList = nullptr;
if (mImg== nullptr || sImg== nullptr || nMatches == nullptr) { return nullptr; }
BITMAP mBMP, sBMP;
GetObject(mImg, sizeof(BITMAP), &mBMP);
GetObject(sImg, sizeof(BITMAP), &sBMP);
/*Now, BITMAP mBMP and sBMP are valid*/
return MyList;
}

my c++ and c# interop crashes in 64 bit, why? pointer size?

i got a native 32 bit dll (no source) which runs as a plugin in an application i use. I've done another native dll myself which will communicate with that plugin in order to create and update the plugin's controls.
From that dll i've exported the functions I need in order to control the plugin from my c# application (with p/invoke).
here's the code:
h file:
#pragma once
#include "include\SpoutControls.h"
extern "C" { __declspec(dllexport) void InitializeControls(char *sendername, int *numControls, char** names, int *types, float* floats, float* toggles, float* press, char** text); }
extern "C" { __declspec(dllexport) bool UpdateControls(const char** text, float *floats, float *toggles, float *press, int *numControls); }
extern "C" { __declspec(dllexport) void CloseControls(); }
//
extern "C" __declspec(dllexport) int ReleaseMemory(float *pArray)
{
delete[] pArray;
//delete[] Usize;
return 0;
};
the cpp:
#include "SpoutControls4vvvv.h"
//SpoutControls and the functions
//CreateControl, OpenControls, CheckControls, CloseControls
//are declared in SpoutControls.h, which comes with the 32 bit plugin dll
SpoutControls spoutcontrols;
void InitializeControls(char *sendername, int *numControls, char** names, int *types, float* floats, float* toggles, float* press, char** text) {
int Vcontrols = numControls[0];
int Tcontrols = numControls[1];
int Pcontrols = numControls[2];
int Scontrols = numControls[3];
int all = Vcontrols + Tcontrols + Pcontrols + Scontrols;
int v=0, t=0, p=0, s = 0;
for (int controlID = 0; controlID < all; controlID++) {
if (types[controlID] == 0) {
spoutcontrols.CreateControl(names[controlID], "float",0.0,1.0, floats[v]);
v++;
}
if (types[controlID] == 1) {
spoutcontrols.CreateControl(names[controlID], "bool", toggles[t]);
t++;
}
if (types[controlID] == 2) {
spoutcontrols.CreateControl(names[controlID], "event", press[p]);
p++;
}
if (types[controlID] == 3) {
spoutcontrols.CreateControl(names[controlID], "text", text[s]);
s++;
}
}
spoutcontrols.OpenControls(sendername);
}
bool UpdateControls(const char** text, float *floats, float *toggles, float *press, int *numControls) {
int Vcontrols = numControls[0];
int Tcontrols = numControls[1];
int Pcontrols = numControls[2];
int Scontrols = numControls[3];
int all = Vcontrols + Tcontrols + Pcontrols + Scontrols;
int v = 0, t = 0, p = 0, s = 0;
if (spoutcontrols.CheckControls(myControls)) {
for (int controlID = 0; controlID < all; controlID++) {
if (myControls[controlID].type == 10) {
floats[v] = myControls[controlID].value;
v++;
}
if (myControls[controlID].type == 0) {
toggles[t] = myControls[controlID].value;
t++;
}
if (myControls[controlID].type == 1) {
press[p] = myControls[controlID].value;
p++;
}
if (myControls[controlID].type == 100) {
text[s] = myControls[controlID].text.data();
s++;
}
}
return true;
}
return false;
}
void CloseControls() {
spoutcontrols.CloseControls();
}
and here's the c# code:
public unsafe class SystemSpoutSenderNode: IDisposable
{
[System.Runtime.InteropServices.DllImport("SpoutControls4vvvv.dll")]
private static extern void InitializeControls(IntPtr sendername, IntPtr numControls,String[] names, IntPtr types, IntPtr floats, IntPtr toggles, IntPtr press, String[] text);
[System.Runtime.InteropServices.DllImport("SpoutControls4vvvv.dll")]
private static extern int CloseControls();
[System.Runtime.InteropServices.DllImport("SpoutControls4vvvv.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern bool UpdateControls([In, Out] String[] text, [In, Out] float[] floats, [In, Out] float[] toggles, [In, Out] float[] press, IntPtr numControls);
[System.Runtime.InteropServices.DllImport("SpoutControls4vvvv.dll")]
private static extern int ReleaseMemory(IntPtr ptr);
public void Evaluate(int SpreadMax)
{
//countControls determines number of controls per type (string,float,toggle,click)
int[] controls = countControls(FType);
//sumControls will just add up all elements in controls
int all = sumControls(controls);
//in my code these arrays will get filled with values, deleted here for readability
String[] names = new String[all];
int[] types = new int[all];
float[] floats = new float[controls[0]];
float[] toggles = new float[controls[1]];
float[] press = new float[controls[2]];
String[] text = new String[controls[3]];
//initialze return arrays
String[] Rtext = new String[controls[3]];
float[] Rfloats = new float[controls[0]];
float[] Rtoggles = new float[controls[1]];
float[] Rpress = new float[controls[2]];
//allocate pointers
IntPtr SndrNamePtr = NativeUtf8FromString(FSenderName);
IntPtr BinPtr = Marshal.AllocHGlobal(4*sizeof(int));
IntPtr TypePtr = Marshal.AllocHGlobal(all*sizeof(int));
IntPtr FloatPtr = Marshal.AllocHGlobal(controls[0]*sizeof(float));
IntPtr TogglePtr = Marshal.AllocHGlobal(controls[1]*sizeof(float));
IntPtr PressPtr = Marshal.AllocHGlobal(controls[2]*sizeof(float));
try
{
//copy control info + defaults to pointer
Marshal.Copy(controls, 0, BinPtr, 4);
Marshal.Copy(types, 0, TypePtr, all);
Marshal.Copy(floats, 0, FloatPtr, controls[0]);
Marshal.Copy(toggles, 0, TogglePtr, controls[1]);
Marshal.Copy(press, 0, PressPtr, controls[2]);
//initialize controls
if (FWrite) InitializeControls(SndrNamePtr,BinPtr,names,TypePtr,FloatPtr,TogglePtr,PressPtr,text);
//update controls
bool changed = UpdateControls(Rtext,Rfloats,Rtoggles,Rpress,BinPtr);
//FF, FT, FS and FP are the outputs in my c# host
if (changed){
for(int j=0; j<controls[0];j++){
FF[j]=Rfloats[j];
}
for(int j=0; j<controls[1];j++){
FT[j]=FloatToBool(Rtoggles[j]);
}
for(int j=0; j<controls[3];j++){
FS[j]=Rtext[j];
}
}
for(int j=0; j<controls[2];j++){
FP[j]=FloatToBool(Rpress[j]);
}
}
finally
{
Marshal.FreeHGlobal(SndrNamePtr);
Marshal.FreeHGlobal(BinPtr);
Marshal.FreeHGlobal(FloatPtr);
Marshal.FreeHGlobal(TogglePtr);
Marshal.FreeHGlobal(PressPtr);
}
}
}
}
public void Dispose()
{
CleanUp();
CloseControls();
}
}
NOTE: the c# code runs without precompiling in a frame-based, c# host environment for graphical programming (vvvv), therefore i've deleted host specific decalarations of inputs (FType,FSenderName) and outputs (FF,FS,FP,FT) to avoid confusion. These will be used to "connect" this code with other functionality. Evaluate will be called every frame by the host.
Now to the actual question(s):
it's working fine so far in 32 bit, but in 64 bit my c# host crashes without any message. after some reading i believe this is due to pointer sizes being different in 32/64bit systems, but i'm not exactly sure what to do/if this actually applies here. I would be very thankful if you could
explain me how (and why) to get this code to run in 64 bit
point out any other mistakes you might spot along the way- i'm completely new to c++ and still a beginner in c#, so i'm pretty confident there's a lot to improve here; especially: memory leaks and passing the values from c++ to c# and vice versa...uiuiui.
I've understood that I shouldn't cast a pointer to an int in 64 bit, so the last thing I've tried is to change from
int Vcontrols = numControls[0];
int Tcontrols = numControls[1];
int Pcontrols = numControls[2];
int Scontrols = numControls[3];
to
int Vcontrols = (INT_PTR)numControls[0];
int Tcontrols = (INT_PTR)numControls[1];
int Pcontrols = (INT_PTR)numControls[2];
int Scontrols = (INT_PTR)numControls[3];
but with no luck, therefore I'm posting my original problem, even if this is a correct improvement(?).
EDIT: thanks to #dkackman for pointing out one unclear point: my cpp code calls functions which come as source code (SpoutControls.h) with the native 32 bit dll. It's not the source for the 32 bit dll itself but declares the functions used to (as far as i can tell) access the same shared memory as the 32 bit dll.
I can also copy paste the code here if this might be the problem?
Also can be found here
thank you.
I am afraid you are out of luck. If your process is 64bit, you won't be able to load that 32bit dll, no matter how much you try.
Can I load a 32 bit DLL into a 64 bit process on Windows?
from https://msdn.microsoft.com/en-us/library/windows/desktop/aa384231(v=vs.85).aspx
On 64-bit Windows, a 64-bit process cannot load a 32-bit dynamic-link
library (DLL).
Without access to its source, your only option would be to convert your host to 32bit or otherwise figure out how to host the 32bit plugin in a 32bit process and use some sort of IPC to communicate with it from a 64bit host process.
So my guess is that this has nothing to do with your wrapper, array passing or interop code.

Return SAFEARRAY from c++ to c#

I have a c++ method which creates, fills and returns SAFEARRAY:
SAFEARRAY* TestClass::GetResult(long& size)
{
return GetSafeArrayList(size);
}
How should I export that function in a DLL so that c# could take it
How should I write c# method signature?
I have in c++ something along these lines:
extern "C" __declspec(dllexport) void GetResult(SAFEARRAY*& data, long& size)
{
size = 0;
data = handle->GetResult(size);
}
Is it correct, isn't it?
Thanks for help!
EDIT:
c# call:
public static extern void GetResult(IntPtr handle, [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_USERDEFINED)] TestStruct[] data, ref int size);
Full example of use of a SAFEARRAY(int) C#->C++->C# (so the array is initialized with some data in C#, passed to C++, modified there and returned to C#).
C++:
// For the various _t classes for handling BSTR and IUnknown
#include <comdef.h>
struct ManagedUDT
{
BSTR m_str01;
int m_int01;
~ManagedUDT()
{
::SysFreeString(m_str01);
m_str01 = NULL;
}
};
extern "C" __declspec(dllexport) void GetResult(SAFEARRAY*& data)
{
if (data != NULL)
{
// Begin print content of SAFEARRAY
VARTYPE vt;
HRESULT hr = SafeArrayGetVartype(data, &vt);
if (SUCCEEDED(hr))
{
// To make this code simple, we print only
// SAFEARRAY(VT_I4)
if (vt == VT_I4)
{
int *pVals;
hr = SafeArrayAccessData(data, (void**)&pVals); // direct access to SA memory
if (SUCCEEDED(hr))
{
long lowerBound, upperBound; // get array bounds
SafeArrayGetLBound(data, 1, &lowerBound);
SafeArrayGetUBound(data, 1, &upperBound);
long cnt_elements = upperBound - lowerBound + 1;
for (int i = 0; i < cnt_elements; i++) // iterate through returned values
{
int val = pVals[i];
printf("C++: %d\n", val);
}
SafeArrayUnaccessData(data);
}
else
{
// Error
}
}
}
else
{
// Error
}
// End print content of SAFEARRAY
// Delete the SAFEARRAY if already present
SafeArrayDestroy(data);
data = NULL;
}
{
// Creation of a new SAFEARRAY
SAFEARRAYBOUND bounds;
bounds.lLbound = 0;
bounds.cElements = 10;
data = SafeArrayCreate(VT_I4, 1, &bounds);
int *pVals;
HRESULT hr = SafeArrayAccessData(data, (void**)&pVals); // direct access to SA memory
if (SUCCEEDED(hr))
{
for (ULONG i = 0; i < bounds.cElements; i++)
{
pVals[i] = i + 100;
}
}
else
{
// Error
}
}
}
C#
[DllImport("NativeLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void GetResult([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_I4)] ref int[] ar);
and
var data = new int[] { 1, 2, 3, 4, 5 };
GetResult(ref data);
if (data != null)
{
for (int i = 0; i < data.Length; i++)
{
Console.WriteLine("C#: {0}", data[i]);
}
}
else
{
Console.WriteLine("C#: data is null");
}
Code partially taken from https://stackoverflow.com/a/12484259/613130 and https://stackoverflow.com/a/3735438/613130
SAFEARRAY(VT_RECORD)
It is doable... Very hard... but doable. Please don't do it. You can't hate enough the world to do it. I do hope you don't!
C++:
// For the _com_util
#include <comdef.h>
extern "C"
{
__declspec(dllexport) void GetResultSafeArray(SAFEARRAY *&psa)
{
// All the various hr results should be checked!
HRESULT hr;
// Begin sanity checks
if (psa == NULL)
{
// Error
}
VARTYPE pvt;
hr = ::SafeArrayGetVartype(psa, &pvt);
if (pvt != VT_RECORD)
{
// Error
}
UINT size;
size = ::SafeArrayGetElemsize(psa);
if (size != sizeof(ManagedUDT))
{
// Error
}
// From tests done, it seems SafeArrayGetRecordInfo does a AddRef
_com_ptr_t<_com_IIID<IRecordInfo, NULL> > prinfo;
// The_com_ptr_t<>::operator& is overloaded
hr = ::SafeArrayGetRecordInfo(psa, &prinfo);
// From tests done, it seems GetName returns a new instance of the
// BSTR
// It is ok to use _bstr_t.GetAddress() here, see its description
_bstr_t name1;
hr = prinfo->GetName(name1.GetAddress());
const _bstr_t name2 = _bstr_t(L"ManagedUDT");
if (name1 != name2)
{
// Error
}
// End sanity checks
long lowerBound, upperBound; // get array bounds
hr = ::SafeArrayGetLBound(psa, 1, &lowerBound);
hr = ::SafeArrayGetUBound(psa, 1, &upperBound);
long cnt_elements = upperBound - lowerBound + 1;
// Begin print
ManagedUDT *pVals;
hr = ::SafeArrayAccessData(psa, (void**)&pVals);
printf("C++:\n");
for (int i = 0; i < cnt_elements; ++i)
{
ManagedUDT *pVal = pVals + i;
// If you are using a recent VisualC++, you can
// #include <memory>, and then
//std::unique_ptr<char[]> pstr(_com_util::ConvertBSTRToString(pVal->m_str01));
// and you don't need the char *pstr line and the delete[]
// line
char *pstr = _com_util::ConvertBSTRToString(pVal->m_str01);
printf("%s, %d\n", pstr, pVal->m_int01);
delete[] pstr;
}
hr = ::SafeArrayUnaccessData(psa);
// End print
// Begin free
SAFEARRAYBOUND sab;
sab.lLbound = 0;
sab.cElements = 0;
// SafeArrayRedim will call IRecordInfo::RecordClear
hr = ::SafeArrayRedim(psa, &sab);
// End Free
// Begin create
int numElements = 10;
sab.cElements = numElements;
hr = ::SafeArrayRedim(psa, &sab);
hr = ::SafeArrayAccessData(psa, (void**)&pVals);
for (int i = 0; i < numElements; i++)
{
ManagedUDT *pVal = pVals + i;
char pstr[100];
sprintf(pstr, "Element #%d", i);
pVal->m_str01 = _com_util::ConvertStringToBSTR(pstr);
pVal->m_int01 = 100 + i;
}
hr = ::SafeArrayUnaccessData(psa);
// End create
}
__declspec(dllexport) void GetResultSafeArrayOut(SAFEARRAY *&psa, ITypeInfo *itypeinfo)
{
// All the various hr results should be checked!
HRESULT hr;
// Begin sanity checks
if (psa != NULL)
{
// Begin free
// SafeArrayDestroy will call IRecordInfo::RecordClear
// if necessary
hr = ::SafeArrayDestroy(psa);
// End Free
}
// Begin create
int numElements = 10;
SAFEARRAYBOUND sab;
sab.lLbound = 0;
sab.cElements = numElements;
// The_com_ptr_t<>::operator& is overloaded
_com_ptr_t<_com_IIID<IRecordInfo, NULL> > prinfo;
hr = ::GetRecordInfoFromTypeInfo(itypeinfo, &prinfo);
psa = ::SafeArrayCreateVectorEx(VT_RECORD, 0, numElements, prinfo);
ManagedUDT *pVals;
hr = ::SafeArrayAccessData(psa, (void**)&pVals);
for (int i = 0; i < numElements; i++)
{
ManagedUDT *pVal = pVals + i;
char pstr[100];
sprintf(pstr, "Element #%d", i);
pVal->m_str01 = _com_util::ConvertStringToBSTR(pstr);
pVal->m_int01 = 100 + i;
}
hr = ::SafeArrayUnaccessData(psa);
// End create
}
}
C#:
[ComVisible(true)]
[Guid("BBFE1092-A90C-4b6d-B279-CBA28B9EDDFA")]
[StructLayout(LayoutKind.Sequential)]
public struct ManagedUDT
{
[MarshalAs(UnmanagedType.BStr)]
public string m_str01;
public Int32 m_int01;
}
[DllImport("NativeLibrary.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
static extern void GetResultSafeArray([MarshalAs(UnmanagedType.SafeArray)] ref ManagedUDT[] array);
[DllImport("NativeLibrary.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
static extern void GetResultSafeArrayOut([MarshalAs(UnmanagedType.SafeArray)] out ManagedUDT[] array, IntPtr itypeinfo);
[DllImport("NativeLibrary.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "GetResultSafeArrayOut")]
static extern void GetResultSafeArrayRef([MarshalAs(UnmanagedType.SafeArray)] ref ManagedUDT[] array, IntPtr itypeinfo);
and
var arr = new[]
{
new ManagedUDT { m_str01 = "Foo", m_int01 = 1},
new ManagedUDT { m_str01 = "Bar", m_int01 = 2},
};
{
Console.WriteLine("C#:");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("{0}, {1}", arr[i].m_str01, arr[i].m_int01);
}
}
{
Console.WriteLine();
var arr2 = (ManagedUDT[])arr.Clone();
GetResultSafeArray(ref arr2);
Console.WriteLine();
Console.WriteLine("C#:");
for (int i = 0; i < arr2.Length; i++)
{
Console.WriteLine("{0}, {1}", arr2[i].m_str01, arr2[i].m_int01);
}
}
{
Console.WriteLine();
ManagedUDT[] arr2;
IntPtr itypeinfo = Marshal.GetITypeInfoForType(typeof(ManagedUDT));
GetResultSafeArrayOut(out arr2, itypeinfo);
Console.WriteLine();
Console.WriteLine("C#:");
for (int i = 0; i < arr2.Length; i++)
{
Console.WriteLine("{0}, {1}", arr2[i].m_str01, arr2[i].m_int01);
}
}
{
Console.WriteLine();
var arr2 = (ManagedUDT[])arr.Clone();
IntPtr itypeinfo = Marshal.GetITypeInfoForType(typeof(ManagedUDT));
GetResultSafeArrayRef(ref arr2, itypeinfo);
Console.WriteLine();
Console.WriteLine("C#:");
for (int i = 0; i < arr2.Length; i++)
{
Console.WriteLine("{0}, {1}", arr2[i].m_str01, arr2[i].m_int01);
}
}
There is a single big caveat for GetResultSafeArray: you must pass from C# at least an empty array (like a new ManagedUDT[0]). This because to create a SAFEARRAY(ManagedUDT) from nothing in C++ you would need a IRecordInfo object. I don't know how to retrieve it from C++. If you already have a SAFEARRAY(ManagedUDT) then clearly it has the IRecordInfo already set, so there is no problem. In the example given, in C++ there are first some sanity checks, then the passed array is printed, then it is emptied, then it is re-filled. The GetResultSafeArrayOut/GetResultSafeArrayRef "cheat": they receive from C# a ITypeInfo pointer (that is easy to retrieve in C#, with Marshal.GetITypeInfoForType()), and from taht the C++ can retrieve the IRecordInfo interface.
Some notes:
I wrote Ansi-charset-C++. Normally for myself I always write Unicode-ready C++ (or directy Unicode-C++, because all the Windows NT support Unicode), but I've noticed that I'm an exception... So in various parts of the code there are conversions BSTR->Ansi->BSTR.
I'm retrieving the HRESULT of all the function calls. They should be checked, and the failure handled.
The most complex thing in C++/COM is knowing when to free something... In general always free/Release() everything! (be it BSTR/IUnknown derived interfaces, ...)
Unless there is a bug, there is no support for this code. Consider it to be a proof of concept. I already lost various hours on it out of curiosity. You break it, you repair it.

Converting from C: fputc and fwrite in C#?

Question: In order to write a C# interface to libespeak, I need to convert the callback SynthCallback to C#.
See the below C code.
You might need this for reference:
https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/wave.html
Basically,
espeak_initialize
espeak_SetSynthCallback(SynthCallback);
espeak_SetParameter(espeakRATE, 510, 0);
espeak_Synth(".", 20, 0, POS_CHARACTER, 0, 0, NULL, NULL);
are DllImport-ed function.
And I already have them working asynchronously, without file.
Now I want to get the synchronous version working with files, but I have a little problem:
The callback function
static int SynthCallback(short *wav, int numsamples, espeak_EVENT *events)
First, I need to create a delegate for that I can pass this function to the C dll/so.
Which isn't a problem, but if I make short *wav to a System.IntPtr, how do I write the data to a file ?
In other words: Can somebody help me with fwrite, fputc, Write4Bytes converting this into proper C#?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <espeak/speak_lib.h>
// gcc -o mine speak.cpp -I/usr/include/espeak/ -lespeak
FILE *f_wavfile = NULL;
static int SynthCallback(short *wav, int numsamples, espeak_EVENT *events);
// Write 4 bytes to a file, least significant first
static void Write4Bytes(FILE *f, int value)
{
int ix;
for(ix=0; ix<4; ix++)
{
fputc(value & 0xff,f);
value = value >> 8;
}
}
int OpenWavFile(char *path, int rate)
{
static unsigned char wave_hdr[44] = {
'R','I','F','F',0x24,0xf0,0xff,0x7f,'W','A','V','E','f','m','t',' ',
0x10,0,0,0,1,0,1,0, 9,0x3d,0,0,0x12,0x7a,0,0,
2,0,0x10,0,'d','a','t','a', 0x00,0xf0,0xff,0x7f
};
if(path == NULL)
return(2);
if(path[0] == 0)
return(0);
if(strcmp(path,"stdout")==0)
f_wavfile = stdout;
else
f_wavfile = fopen(path,"wb");
if(f_wavfile == NULL)
{
fprintf(stderr,"Can't write to: '%s'\n",path);
return(1);
}
fwrite(wave_hdr, 1, 24, f_wavfile);
Write4Bytes(f_wavfile, rate);
Write4Bytes(f_wavfile, rate * 2);
fwrite(&wave_hdr[32], 1, 12, f_wavfile);
return(0);
} // end of OpenWavFile
static void CloseWavFile()
{
unsigned int pos;
if((f_wavfile==NULL) || (f_wavfile == stdout))
return;
fflush(f_wavfile);
pos = ftell(f_wavfile);
fseek(f_wavfile, 4, SEEK_SET);
Write4Bytes(f_wavfile, pos - 8);
fseek(f_wavfile, 40, SEEK_SET);
Write4Bytes(f_wavfile, pos - 44);
fclose(f_wavfile);
} // end of CloseWavFile
int main()
{
char buf[22050];
int i = 0;
memset(&buf, 0, sizeof(buf));
// OpenWavFile((char*) "test.wav", 22050);
int SampleRate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, 300, NULL, 0);
OpenWavFile((char*) "test.wav", SampleRate);
espeak_SetSynthCallback(SynthCallback);
//espeak_SetParameter(espeakRATE, 510, 0);
//espeak_SetParameter(espeakRANGE, 75, 0);
for (i=0; i < 9;i++)
{
/*
espeak_ERROR espeak_Synth(
const void *text,
size_t size,
unsigned int position,
espeak_POSITION_TYPE position_type,
unsigned int end_position,
unsigned int flags,
unsigned int* unique_identifier,
void* user_data);
*/
//espeak_POSITION_TYPE.POS_CHARACTER
espeak_Synth("test", 10, 0, POS_CHARACTER, 0, 0, NULL, NULL);
fwrite(buf, 1, 5512, f_wavfile);
espeak_Synth(".", 20, 0, POS_CHARACTER, 0, 0, NULL, NULL);
fwrite(buf, 1, 22050, f_wavfile);
}
CloseWavFile();
}
static int SynthCallback(short *wav, int numsamples, espeak_EVENT *events)
{
if (wav == NULL)
return 0;
if (numsamples > 0)
{
fwrite(wav, numsamples * 2, 1, f_wavfile);
}
return 0;
}
FileStream sink;
Int32 SynthCallback(IntPtr wav, Int32 numsamples, IntPtr events)
{
var wavm = new Int16[numsamples];
Marshal.Copy(wav, wavm, 0, numsamples);
// and do something with wavm
//or
var wavbytes = new Byte[numsamples * 2];
Marshal.Copy(wav, wavbytes, 0, numsamples*2);
sink.Write(wavbytes, 0, numsamples*2);
}
For writing 32-integers, you can either use BitConverter.GetBytes and FileStream.Write or maybe BinaryWriter.
You could P/Invoke CreateFile/WriteFile to write the IntPtr directly.

Categories