converting lpwstr to const char [duplicate] - winapi

This question already has answers here:
How do you convert LPCWSTR to const char *?
(2 answers)
c++ convert from LPCTSTR to const char *
(5 answers)
Closed 9 years ago.
I have made use of function gethostbyname function to get the host information.The function expects a const char * string but i have a string of type LPWSTR and hence i'm getting the error as
error C2664: 'gethostbyname' : cannot convert parameter 1 from 'LPWSTR' to 'const char *'
i tried performing a type cast from LPWSTR to const char * as below but the compiler errors seem to go but its not giving the intended result .
host = gethostbyname((const char *)resource.lpRemoteName);
i followed up google and made use of Cstring as below for the same but the the function seems to fail.Please let me know the conversion .
CString strFullName =resource.lpRemoteName;
host = gethostbyname(strFullName);

Related

issue with <libgen.h> and using basename

Unable to get basename to work in C++, I am trying to just get the code to compile.
This is for a school assignment and I have tried to see if it is my stdlib having issues -- however, it seems that the problem is that
'''c++
#include<iostream>
#include<cstring>
#include<libgen.h>
int main(){
const char *ch = "asdfasdf/asdf.cpp";
std::cout<<basename(ch) << std::endl;
std::cout<<dirname(strdup(ch)) << std::endl;
return 0;
}
I was supposed to print out the output of ch, however, instead I get the error message:
test.cpp:8:14: error: no matching function for call to 'basename'
std::cout<enter code here
/usr/include/libgen.h:40:7: note: candidate function not viable: 1st argument ('const char *') would lose const qualifier
char *basename(char *);
^
1 error generated.
basename's argument is char*, so this function may modify passed data. You cannot pass const char* - pointer to only-read data into basename because it would let basename modify const data - not allowed.
Create your input as modifiable array:
char ch[] = "asdfasdf/asdf.cpp";

passing argument 1 of 'strlen' makes pointer from integer without a cast

It is not clear why I get a warning of:
[Warning] passing argument 1 of 'strlen' makes pointer from integer without a cast [enabled by default]
expected 'const char *' but argument is of type 'char'
on two of the 3 statements containing strlen() below.
Even when I attempted to cast *str it still gave the same warning.
bfr is a character buffer. *str points to that char buffer after the call to
gets(). If I use strlen(*str) I get a warning. If I use strlen(bfr) I do not.
But *str should be the equivalent to bfr. Thus the confusion regarding the error.
Now in reality, strlen arg 1 is defined as strlen(const char *string). So I
would have expected strlen(bfr) to also produce an error since bfr[] is a
char string and not a const char either.
And where is the integer that is being made into a pointer?
I am using gcc under wXDev-C++.
void test(){
FILE *fileID = fopen("somefile.txt","r");
char *str, len;
char bfr[16];
str = fgets(bfr,16,fileID); // str will be set equal to &bfr[0]
len = strlen(*str); // This gives a warning
len = strlen((const char)*str); // This gives a warning
len = strlen(bfr); // This does not give a warning
}
Sometimes you just need to take a fresh look in the morning at a problem. I realized that strlen is looking for a pointer to a string and 'str' is defined as a pointer. So *str would be a pointer to a pointer. So the warning was correct. It should read len = strlen(s) not len = strlen(*s). And it is 'str' pointing to 'bfr' not *str;
Answered my own question.

cannot convert parameter from 'PCHAR' to 'LPCWSTR'

I have defined the pointer size_Drive as :
PCHAR size_Drive ;
then i used the function lstrlen :
size_Drive += (lstrlen(size_Drive) + 1) ; (line 28)
but it gives me the following ERROR :
1>c:\users\hp.hp-pc\documents\visual studio 2008\projects\getvolumeinfo\getvolumeinfo\getvolumeinfo.cpp(28) : error C2664: 'lstrlenW' : cannot convert parameter 1 from 'PCHAR' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
PCHAR is a typedef for char whereas LPCWSTR is a typedef for const wchar_t*, and in a Unicode build lstrlen is a macro for the Unicode function lstrlenW.
You should call lstrlenA specifically to use the ANSI function.

opengl tutorial xcode cmake

I followed every instruction on OpenGL-tutorial.org, downloading the 2.1 code, running it through cmake specifying xcode and using its default compilers. I configured till the red lines were gone then clicked generated. The project file it generated though, when opened in xcode displays the following errors:
/OpenGL-tutorial_v0010_21/external/AntTweakBar-1.15/src/LoadOGLCore.h:149:24: Conflicting types for 'glMultiDrawElements'
/OpenGL-tutorial_v0010_21/external/AntTweakBar-1.15/src/LoadOGLCore.h:214:24: Conflicting types for 'glShaderSource'
/external/AntTweakBar-ObjectiveC/LoadOGLCore.mm:116:1: Cannot initialize a variable of type 'PFNglMultiDrawElements' (aka 'void (*)(GLenum, const GLsizei *, GLenum, const GLvoid **, GLsizei)') with an lvalue of type 'void (GLenum, const GLsizei *, GLenum, const GLvoid *const *, GLsizei)': type mismatch at 4th parameter ('const GLvoid **' (aka 'const void **') vs 'const GLvoid *const *' (aka 'const void *const *'))
/external/AntTweakBar-ObjectiveC/LoadOGLCore.mm:116:1: Cannot initialize a variable of type 'PFNglMultiDrawElements' (aka 'void (*)(GLenum, const GLsizei *, GLenum, const GLvoid **, GLsizei)') with an lvalue of type 'void (GLenum, const GLsizei *, GLenum, const GLvoid *const *, GLsizei)': type mismatch at 4th parameter ('const GLvoid **' (aka 'const void **') vs 'const GLvoid *const *' (aka 'const void *const *'))
I am using Xcode 4.6.1 and cmake 2.8-10
Could this be something wrong with cmake or the tutorials code?
The first two errors are easy enough to fix, maybe it will fix the other two. Notice the signature in gl.h for glShaderSource. It is:
extern void glShaderSource (GLuint shader, GLsizei count, const GLchar* const *string, const GLint *length);
The third parameter is a 'const pointer to a const pointer to string.'
However, notice that the signature in LoadOGLCore.h is (expanding the macro):
extern "C" { void APIENTRY glShaderSource (GLuint shader, GLsizei count, const GLchar * *string, const GLint *length);
The third parameter is a 'const pointer to a string pointer' (missing the 'const'). So, open up LoadOGLCore.h and change (on line 214):
const GLchar* *string
to:
const GLchar* const *string
Do the same for the other error regarding glMultiDrawElements. This allowed me to at least build AntTweakBar on OSX 10.8.5. Still haven't tried to run a sample project yet, but I'll update when I do.
go to the definition of those functions and replace the prototype with the correct (update) parameter definitions
Author here,
Welcome on StackOverflow, and thanks for reading opengl-tutorial.org !
This is a known issue (on 10.8 only, which I don't have). However, AntTweakBar, which provides a simple GUI, is only used for the very last tutorial. Other sub-projects should build just fine. Just in case, you can remove lines 367 to 385 in the CMakeLists.txt file, and rerun CMake.
I'll update this post when it'll be fixed. Sorry for the inconvenience !

MFC CString to int

using this code i get an error at atoi's conversion from CString to int:
int nrCentrala; CString idCentrala;
GetDlgItem(IDC_EDIT_IDCENTRALA)->GetWindowText(idCentrala);
nrCentrala = atoi(&idCentrala);
where IDC_EDIT_IDCENTRALA is an edit control.
Error 7 error C2664: 'atoi' : cannot convert parameter 1 from 'CString *' to 'const char *'
CString provides an overloaded operator for LPCTSTR. Also, use _tstoi instead, just in case you're compiling with Unicode libraries, e.g.
nrCentrala = _tstoi(idCentrala);
You pass pointer to CString (CString* type) instead of const char* which is expected by atoi(). Correct call is nrCentrala = atoi(idCentrala.GetString());

Resources