I'm using C++/CLI and I have a raw HBITMAP called hStrip, I have to display this in a PictureBox, so I found the following function System::Drawing::Image::FromHbitmap, however this takes IntPtr and I wondered if I need to convert my raw HBITMAP to IntPtr before passing it in or if I could get away passing it in like this?
If I need to convert, how should I convert? I haven't really found the conversion this way. I did find it the other way though.
A simple cast gets the job done:
HBITMAP hBmp = NULL;
// Assign hBmp
//...
Bitmap^ bmp = Bitmap::FromHbitmap((IntPtr)hBmp);
DeleteObject(hBmp); // okay to destroy now
Related
I have a "CLock.dll" have some functions
For example: This is document for a function
__int16 __stdcall dv_get_auth_code(unsigned char* auth);
Function
To gain authorization code of setup card.
Parameters
auth:[out] Return authorization code, 6 characters.
Return
Succeed then return 0.
I need to call this dll in my winform application. I try
[DllImport("CLock.dll",CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int dv_get_auth_code([Out]StringBuilder auth);`
and in Main()
My code:
StringBuilder sb = new StringBuilder();
int result = dv_get_auth_code(sb);
But it's working. What should i do? Thank you and have a nice day !
There are two mistakes in the code as presented. The return type is wrong, and no buffer is allocated.
The return type is a 16 bit type, in C# that is short:
[DllImport("Clock.dll")]
public static extern short dv_get_auth_code(StringBuilder auth);
Then to call the function you need to allocate a buffer. I don't know how large that buffer should be, presumably you know that.
StringBuilder sb = new StringBuilder(bufferLengtg);
short result = dv_get_auth_code(sb);
It is always wise for such an API to pass the length of the buffer to the function. Then it can make sure it does not overrun the buffer.
I have the following function from a dll:
aisgdll_setinfo(int dev, set_field_code field, void *data);
I know how to deal with the first two parameters. I have a textbox the user enters data into and the textbox returns a variable of type String^. I somehow need to get the data from that textbox and do something, so that I can write it to this function to the void *data parameter.
You can use this form.
I used the Thread :: Sleep to prevent abuse in processing. While your thread does not change the status of the screen will be updated.
void solve()
{
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Threading;
Thread^ bot_thread = gcnew Thread(gcnew ThreadStart(bot_run));
bot_thread->Start();
PictureBox^ PB_LoadGIF = gcnew PictureBox();
PB_LoadGIF->Visible = true;
while (bot_thread->ThreadState == ThreadState::Running)
{
Thread::Sleep(1000);
PB_LoadGIF->Parent->Refresh();
}
}
After further research online, I have found some solutions to the problems I was having. The functions I was plugging the retrieved values into needed pointers. I think these solutions can be adapted by others for their own purposes. For converting the Visual Studio Text box String^ to char*:
char* iData = (char*)Marshal::StringToHGlobalAnsi(activeBox->Text).ToPointer();
When I needed to get a floating point number from a Text box, I used the following:
int anInteger = (int)((Convert::ToDouble(activeBox->Text))*10);
int *iData = &anInteger;
I am trying to convert a handle string to a normal string. I though the method I was using was working, but when I look in the debugger it appears that half of my string has been chopped off on the line that creates the chars variable. Any idea why and what the proper way to convert a handle string to a normal string woudl be?
std::string convert(String^ s) {
const char* chars = (const char*)(System::Runtime::InteropServices::Marshal::
StringToHGlobalAnsi(s)).ToPointer();
string myNewString = std::string(chars);
return myNewString;
}
It's probably the debugger that's cutting off the display of the string. You didn't mention how long a string you're using, but the debugger can't display infinite length, so it has to cut it off at some point.
To verify this, try printing myNewString to the console, or to the debugger via Debug::WriteLine or OutputDebugString.
However, there is a significant issue in your code: After allocating memory with StringToHGlobalAnsi, you must free it using FreeHGlobal.
If you want to continue using StringToHGlobalAnsi, I'd fix it up like this:
std::string convert(String^ s) {
IntPtr ptr = Marshal::StringToHGlobalAnsi(s);
string myNewString = std::string((const char*)ptr.ToPointer());
Marshal::FreeHGlobal(ptr);
return myNewString;
}
However, it's probably easier to use the marshal_as methods. This will take care of everything for you.
std::string output = marshal_as<std::string>(managedString);
I am trying to build a dll that reads a text file to populate a 2d array, then change that array as needed. I'm using a VB GUI to access it. The overall program is a micromouse simulator in which the user is able to customize the wall placement in a 5x5 maze, as well as mouse start position and goal placement, and allow the search algorithm (dll) to solve it. Here's the code inside my dll:
/*testDLL.cpp*/
#include "testDLL.h"
#include <stdio.h>
FILE *maze;
char mazearray[12][12];
void _stdcall wallfunction(int x, int y){
maze = fopen ("C:\Users\Public\Documents\5x5mazedefault.txt", "r");
fread (mazearray, sizeof(mazearray), 1, maze);
fclose(maze);
if (mazearray[x][y] == 'X'){
mazearray[x][y] = ' ';
}
else if (mazearray[x][y] == ' '){
mazearray[x][y] = 'X';
}
}
I want to be able to put in two input variables as the index of the matrix and add or subtract a wall from that location. Whenever I try to call the function from VB, it sends me a message: PInvoke restriction cannot return variants. The function returns nothing, so I don't understand...
Here's the declaration statement inside my VB program:
Private Declare Function wallfunction Lib "C:\Path\Path\testDLL.dll" (ByVal x As Integer, ByVal y As Integer)
I'm aware I'm not going to be able to call the fread function everytime the user wants to change a wall; I'm just trying to get this working once first. Any thoughts?
Change Function to Sub in your Declare statement in VB. This is because your C++ function returns void.
I'm trying to embed and then play back a .wav file in a C++/CLI app but all the examples I've seen which use PlaySound are in VB. I can't see how to get froma Stream^ to the LPCSTR which PlaySound requires:
System::IO::Stream^ s = Assembly::GetExecutingAssembly()->GetManifestResourceStream ("Ping.wav");
LPCSTR buf = s->????;
PlaySound(buf, NULL, SND_ASYNC|SND_MEMORY|SND_NOWAIT);
I guess I need some sort of horrible .net memory conversion magic.
Use the System::Media::SoundPlayer class instead. It has a Stream property, assign your "s" variable to it, then call the Play() method.