How to Convert char* to LPWSTR in VC++? - visual-c++

How to Convert char* to LPWSTR in VC++ ?
LPNETRESOURCEW nr = NULL;
memset(&nr, 0, sizeof (NETRESOURCE));
nr->lpLocalName = strDriveLetter.GetBuffer(strDriveLetter.GetLength()); // this line giving me error "Cannot Convert char* to LPWSTR"
Any help is appreciated.
Thanks.

Use MultiByteToWideChar function;
const char* msg = "foobarbaz";
int len = strlen(msg) + 1;
wchar_t *w_msg = new wchar_t[len];
memset(w_msg, 0, len);
MultiByteToWideChar(CP_ACP, NULL, msg, -1, w_msg, len);

memset(&nr, 0, sizeof (NETRESOURCE)); here nr is a NULL pointer. This is not correct. You should have nr point to a valid memory first by either using explicit allocation like new or on allocate on stack.

Related

Linux Kernel Module : Invalid output in the file for kernel_write function

I'm printing a sample string using kernel_write function and getting the output but I'm not sure about the format that it's printing in the file
Here is my code:
char* res = "xyz";
char* buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
loff_t pos = 0;
size_t resw;
char* file i_fp = filp_open("input.txt",O_WRONLY | O_CREAT | O_APPEND, 0777);
if (!IS_ERR(i_fp)) {
resw = kernel_write(i_fp, (void *)buffer, strlen(res), &pos);
printk("%ld",resw);
printk("ff");
}
output of the file
I want a sample string to be print in the output file
I'm getting the output in the file but the for each character it's showing '\00'. Is there some issue with my code?
Any suggestion is appreciated. Thanks in advance.

Using Lame function hip_decode in Android NDK to decode mp3 return 0

I am using Lame's mpglib to decode mp3 to PCM in Android NDK for playing. But when I called hip_decode(), it returen 0 meaning that "need more data before we can complete the decode". I had no idea how to solve it. Can someone helps me? Here is my code:
void CBufferWrapper::ConvertMp3toPCM (AAssetManager* mgr, const char *filename){
Print ("ConvertMp3toPCM:file:%s", filename);
AAsset* asset = AAssetManager_open (mgr, filename, AASSET_MODE_UNKNOWN);
// the asset might not be found
assert (asset != NULL);
// open asset as file descriptor
off_t start, length;
int fd = AAsset_openFileDescriptor (asset, &start, &length);
assert (0 <= fd);
long size = AAsset_getLength (asset);
char* buffer = (char*)malloc (sizeof(char)*size);
memset (buffer, 0, size*sizeof(char));
AAsset_read (asset, buffer, size);
AAsset_close (asset);
hip_t ht = hip_decode_init ();
int count = hip_decode (ht, (unsigned char*)buffer, size, pcm_l, pcm_r);
free (buffer);
Print ("ConvertMp3toPCM: length:%ld,pcmcount=%d",length, count);
}
I used MACRO "HAVE_MPGLIB" to compile Lame in NDK. So I think it should work for decoding literally.
Yesterday I had the same problem. Is the same problem but using lame_enc.dll. I did not know how to resolve this 0 returned, this is the reason to this post.
Create a buffer to put mp3 data: unsigned char mp3Data[4096]
Create two buffers for pcm data, but bigger than mp3 one:
unsigned short[4096 * 100];
Open mp3 file and initialize hip.
Now, enter in a do while loop until read bytes are 0 (the end of file).
Inside the loop read 4096 bytes into mp3Data and call hip_decode with
hip_decode(ht, mp3Data, bytesRead, lpcm, rpcm);
You are right, it returns 0. It is asking you for more data.
You need to repeat the reading of 4096 bytes and the call to hip_decode until it returns a valid samples number.
Here is the important part of my program:
int total = 0;
int hecho = 0;
int leido = 0;
int lon = 0;
int x;
do
{
total = fread(mp3b, 1, MAXIMO, fich);
leido += total;
x = hip_decode(hgf, mp3b, total, izquierda, derecha);
if(x > 0)
{
int tamanio;
int y;
tamanio = 1.45 * x + 9200;
unsigned char * bu = (unsigned char *) malloc(tamanio);
y = lame_encode_buffer(lamglofla, izquierda, derecha, x, bu, tamanio);
fwrite(bu, 1, y, fichs);
free(bu);
}
}while(total > 0);
My program decodes a mp3 file and encodes the output into another mp3 file.
I expect that this could be useful.

why iconv read more bytes than i specified

I use
size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
to convert UTF-16BE to GB2312.
inbytesleft is bytes number to be convert. After conversion, inbytesleft is bytes number of not converted.
After one call, I found inbytesleft is -2, according to iconv man page this function should read at most inbytesleft.
Who can tell my why and how to fix this?
code to be convert is
"保单验证"
Thanks
How are you getting the input data into your program?
I've tested the situation using this code and it seems to work:
#include <stdio.h>
#include <iconv.h>
#include <errno.h>
int main(){
char data[10] = {0x4f,0xdd,0x53,0x55,0x9a,0x8c,0x8b,0xc1, 0, 0};
char outdata[20];
char *dataptr;
char *outdataptr;
iconv_t cd;
size_t result;
size_t inbytesleft = 8;
size_t outbytesleft = 20;
int i;
cd = iconv_open("GB2312", "UTF-16BE");
dataptr = data;
outdataptr = outdata;
result = iconv(cd, &dataptr, &inbytesleft, &outdataptr, &outbytesleft);
if(result == -1)
printf("Error: %d\n", errno);
printf(" result: %zd\n", result);
printf(" inbytesleft: %zd\n", inbytesleft);
printf("outbytesleft: %zd\n", outbytesleft);
for(i = 20; i > outbytesleft; i--){
if(i != 20)
printf(",");
printf("0x%02x", *((unsigned char *)&(outdata[20-i])));
}
printf("\n");
return 0;
}
It prints
result: 0
inbytesleft: 0
outbytesleft: 12
0xb1,0xa3,0xb5,0xa5,0xd1,0xe9,0xd6,0xa4
Which appears to be correct.
The array of items in the variable data is the UTF-16BE encoding of 保单验证
If this doesn't help, could you post your code for analysis?

C++/CLI String Conversions

I found this really nice piece of code that converts a string to a System:String^ as in:
System::String^ rtn = gcnew String(move.c_str()); // 'move' here is the string
I'm passing rtn back to a C# program. Anyways, inside the function where this code exists, I'm passing in a System::String^. I also found some code to convert a System:String^ to a string using the following code:
pin_ptr<const wchar_t> wch = PtrToStringChars(cmd); // 'cmd' here is the System:String
size_t convertedChars = 0;
size_t sizeInBytes = ((cmd->Length + 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeInBytes);
err = wcstombs_s(&convertedChars,ch, sizeInBytes,wch, sizeInBytes);
Now I can use 'ch' as a string.
This, however, seems to be alot more work than converting the other way using the gcnew. So, at last my question is, is there something out there that will convert a System::String^ to string using a similar fashion as with the gcnew way?
Use VC++'s marshaling library: Overview of Marshaling in C++
#include <msclr/marshal_cppstd.h>
// given System::String^ mstr
std::string nstr = msclr::interop::marshal_as<std::string>(mstr);
this could be useful:
wchar_t *str = "Hi StackOverflow"; //native
String^ mstr= Marshal::PtrToStringAnsi((IntPtr)str); // native to safe managed
wchar_t* A=( wchar_t* )Marshal::StringToHGlobalAnsi(mstr).ToPointer(); // return back to native
don't forget using namespace System::Runtime::InteropServices;

How to print a float number to visual c++ messagebox?

I have, a float number. I would like to print it inside a messagebox. How to do it?
MessageBox(hWnd, "Result = <float>", L"Error", MB_OK);
update:
I do this and it prints out chinese characters inside the messagebox.
float fp = 2.3333f;
sprintf(buffer,"%f",fp);
MessageBox(hWnd, LPCWSTR(buffer), L"Error", MB_OK);
As you are using the wchar_t versions of the Win32-functions you should use swprintf instead of sprintf:
float fp = 2.3333f;
const size_t len = 256;
wchar_t buffer[len] = {};
swprintf(buffer, L"%f", fp);
MessageBox(hWnd, buffer, L"Error", MB_OK);
To avoid potential buffer overruns you could also use _snwprintf:
float fp = 2.3333f;
const size_t len = 256;
wchar_t buffer[len] = {};
_snwprintf(buffer, len - 1, L"%f", fp);
MessageBox(hWnd, buffer, L"Error", MB_OK);
Or better yet, use std::wostringstream declared in <sstream>:
float fp = 2.3333f;
std::wostringstream ss;
ss << fp;
MessageBox(hWnd, ss.str().c_str(), L"Error", MB_OK);
You're using the Unicode version of MessageBox, which is why you have to specify the "Error" string with the L prefix -- this tells it that it should use wide (16-bit) chars. As dalle said, this means you must specify the buffer as wchar_t, and use the corresponding wchar_t version of printf.
You'll be seeing Chinese characters because it's interpreting your string of bytes as a string of wchar_t. You are explicitly casting buffer to be a wchar_t string, after all.
You have to printf the message to a buffer with the %f format code and then use that in your MessageBox()

Resources