无法在WinProc函数中创建mysql ++连接 [英] Can't create mysql++ connection in the WinProc function

查看:75
本文介绍了无法在WinProc函数中创建mysql ++连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我正在编写具有授权功能的Win32应用程序,并使用mySQL服务器存储用户的信息.要连接MySQL,我使用mysqlpp.

有一个问题我整天都无法解决.当我尝试在窗口的WinProc函数中创建mysqlpp :: Connection对象时,在"mlock.c"文件中定义的"_unlock(...)"函数中出现以下错误:

 MyApp(debug).exe中0x74ebb9bc处未处理的异常:Microsoft C ++异常:内存位置0x0020f528处的std :: bad_alloc.

当我在WinMain函数中或在WinMain中创建的我的App类的对象内部创建并使用mysqlpp :: Connection时,它们的工作效果都很好.

这是错误所在的代码:

 // 身份验证
LRESULT CALLBACK AuthWndProcedure(HWND hWindow,UINT uMessage,WPARAM wParam,LPARAM lParam)
{
   开关(uMessage)
   {
   案例 WM_COMMAND:
   {
      开关(LOWORD(wParam))
      {
      案例 IDOK:
         {
         // 如果用户按确定"按钮,则需要检查他在数据库中的名称和密码,因此我需要使用连接这里
         ...

         // 这是错误
         mysqlpp :: Connection sqlConnection( false );
         sqlConnection.connect(g_szSQLDatabase,g_szSQLServer,g_szSQLUser,g_szSQLPass);

         ...
} 



如果有人可以给我一些建议或可以帮助我解决问题,我将非常有责任.

解决方案

由Google搜索
mysql-connector

得到

http://www.mysql.com/products/connector/


适用于MySQL C API的C ++包装程序(MySQL ++)下载
http://gna.org/projects/mysqlpp/


1.获取mysql ++源代码
2.编译msyqlpp.lib,mysqlpp.dll

例如:

 #include   ><   afxwin.h  > 
 #include   <   iostream  > 
 #include   " 
 使用 命名空间 std;
 int  main(){
    字符名称[ 50 ];
    mysqlpp :: Connection conn( false );
    如果(conn.connect(" " " 根"  aaaaaa") ){
        mysqlpp :: Query SetCharacterSetQuery = conn.query(" );
        SetCharacterSetQuery.exec(); // 注意这里还要SetCharacterSetQuery.exec()和C语言的API不同.
        mysqlpp :: Query query = conn.query(" );
        mysqlpp :: StoreQueryResult res = query.store();
        如果(res){
            cout<< res [ 0 ] ["  ]<< ' '<< CW2A(CA2W(res [ 0 ] [" ],CP_UTF8),CP_ACP)<< ' '<< endl;
            cout<< res [ 1 ] ["  ]<< ' '<< CW2A(CA2W(res [ 1 ] [" ],CP_UTF8),CP_ACP)<< ' '<< endl;
        }
    }
    conn.disconnect();
    返回  0 ;
} 


我创建了一个新的空应用程序,并复制了之前制作的该应用程序的所有主要部分.而且有效.我不知道那是什么.包含,lib文件以及项目设置中的所有其他内容都像以前一样.

无论如何,感谢大家的回答和帮助.非常感谢.


Hello everyone.

I''m writing a Win32 application with authorization feature and using mySQL server to store users'' information. To connect mySQL I''m using mysqlpp.

There is a problem that I couldn''t resolve for a whole day. When I''m trying to create mysqlpp::Connection object inside the WinProc function of my window, I get following error in the "_unlock(...)" function defined in the "mlock.c" file:

Unhandled exception at 0x74ebb9bc in MyApp(debug).exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0020f528..



When I''m creating and using mysqlpp::Connection in the WinMain function or inside the object of my App class that being created in the WinMain all works more than fine.

Here is the code where error is:

// Auth wnd
LRESULT CALLBACK AuthWndProcedure(HWND hWindow, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
   switch(uMessage)
   {
   case WM_COMMAND:
   {
      switch( LOWORD( wParam ))
      {
      case IDOK:
         {
         // if users press OK button I need to check his name and password in the database, so i need to use a connection here
         ...

         // here is the error
         mysqlpp::Connection sqlConnection ( false );
         sqlConnection.connect( g_szSQLDatabase, g_szSQLServer, g_szSQLUser, g_szSQLPass );

         ...
}



If someone can give me some advice or can help me with solutuion I''ll be much obliged.

解决方案

search by google
mysql-connector

get

http://www.mysql.com/products/connector/


C++ Wrapper for MySQL C API (MySQL++) Download
http://gna.org/projects/mysqlpp/


1.get mysql++ source
2.compile msyqlpp.lib,mysqlpp.dll

example:

#include <afxwin.h>
#include <iostream>
#include "lib/mysql++.h"
using namespace std;
int main(){
    char name[50];
    mysqlpp::Connection conn(false);
    if (conn.connect("test","192.168.0.175","root","aaaaaa") ) {
        mysqlpp::Query SetCharacterSetQuery = conn.query("SET names 'utf8'");
        SetCharacterSetQuery.exec(); //注意这里还要SetCharacterSetQuery.exec() ,和C语言的API不同.
        mysqlpp::Query query = conn.query("select * from doc_threads");
        mysqlpp::StoreQueryResult res = query.store();
        if (res) {
            cout << res[0]["id"] << ' ' << CW2A(CA2W(res[0]["name"],CP_UTF8),CP_ACP) << ' ' <<endl;
            cout << res[1]["id"] << ' ' << CW2A(CA2W(res[1]["name"],CP_UTF8),CP_ACP) << ' ' <<endl;
        }
    }
    conn.disconnect();
    return 0;
}


I have created new empty app and copied all main parts of the app that I made before. And it works. I don''t know what is was. Includes, lib files and everything else in the project settings is like they was.

Anyway thank you all for answering and for your help. I appreciate it.


这篇关于无法在WinProc函数中创建mysql ++连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆