从vc ++ 2010 App无法访问vector相同的代码在Qt App中可以正常工作. [英] vector not access from vc++2010 App same code works fine in Qt App.

查看:56
本文介绍了从vc ++ 2010 App无法访问vector相同的代码在Qt App中可以正常工作.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有我正在使用的DLL,我已经创建了.它将与webservice交谈并获得一些详细信息.根据网络服务,我已经创建了将近80种API.除了返回矢量元素的所有元素外,其他所有方法都工作正常.我的DLL是在Qt中使用msvc2008进行的纯c ++ cod编译.
我正在检查Qt应用程序,但由于它是平台无关的DLL,因此有必要使用Visual Studio Application进行检查.在这里我遇到了问题.
当我从Qt App调用API时,函数会返回正确的Vector元素.但是从MFC App尝试使用相同的代码不会返回具有某些值的元素,但不会在矢量中显示列表.
如果在MFC中使用向量,我会收到 帖子. MFC中是否有任何需要序列化的类才能填充Values?
我在网上的MFC中没有找到向量的任何序列化类..

Hi,
I have DLL which i am using is i have created. which will talk to webservice and get some details. there are almost 80 API''s i have created depending on webservice. all works fine except for one which returns vector elements. my DLL is pure c++ cod compile with msvc2008 in Qt.
i was checking through Qt application but since it was platform independent DLL it was necessary to check with Visual studio Application. here i encounter with issue.
when i call API from Qt App function returns with proper Vector elements. but same code when tried from MFC App does not return element with some values but do not show list in vector.
i got post in case of using vector in MFC. is there any serialize kind of class require in MFC to populate out Values ?
i did not find any serialize class for vector in MFC on net..

推荐答案

我已经跨DLL/共享库边界返回了向量(和其他STL类型).大量的时间,并没有问题. EXCEPT 当编译器设置有很大不同时 OR DLL是由另一个编译器编译的,在这种情况下所有下注关闭.

当我在注释中找到您的代码时,很难解析此 可能 (CAVEAT,代码未经完全测试,通常我不会这样做,但我正在使用一台没有MFC的计算机[1])可以作为将矢量序列化为档案的一种方法:

I''ve returned vectors (and other STL types) across DLL/shared library boundaries loads of times and not had a problem. EXCEPT when compiler settings were a lot different OR the DLL was compiled with another compiler in which case all bets are off.

As I found your code in the comment a bit hard to parse this might (CAVEAT, code completly untested, normally I don''t do this but I''m using a computer without MFC on it [1]) be workable as a way of serialising a vector into an archive:

template<typename type_to_store>
CArchive &operator<<(
        CArchive &archive,   
        const std::vector<type_to_store> &to_store )
{
    typedef std::vector<type_to_store>::const_iterator iterator;
    for(iterator iter = to_store.begin(); iter != to_store.end(); ++iter )
        archive << *iter;

    return archive;
}



希望任何编译器错误都应该易于解决.如果这两个应用程序都不起作用,那么您有两个应用程序使用了明显不同的编译器设置来构建,唯一的解决方法是尝试找出如何收敛它们.

干杯,



[1]我只有VC ++ 2010 Express,而MFC没有.

添加了guff(16/04/12)

听起来您的问题是,不能保证通常可以链接用两个不同的编译器编译的C ++代码.有时您可以避免使用它,尤其是如果您仅传递纯的(没有实现代码或抽象的)接口类-即该类的所有代码都在DLL中或在调用EXE中.
如果不是这样,则对象的二进制布局在EXE/DLL边界的任何一侧都可能不同,因为不同的编译器以不同的方式编译相同的代码,并且供应商可能已更改了类的实现. MS必须对其标准库进行合理调整,以使其符合VC ++ 2008和2010之间的C ++ 11标准.用2010编译的代码希望找到使用矢量编译的代码的数据指针. 2008编译器可能会将大小固定在同一地址,由于某些原因,实现会交换数据成员,而出于某种原因,它们会围绕它们.

那么,您该怎么办?

您可以尝试的第一件事是传递一个包含vector< whatever>向量的抽象类.并将两个指针导出到向量的开始和结束.

界面如下:



Hopefully any compiler errors should be easy to sort out. If that doesn''t work with both applications then you''ve got two apps that have used significantly different compiler settings to build and the only way around it is to try and work out how to converge them.

Cheers,

Ash

[1] I''ve only got VC++2010 express which doesn''t come with MFC.

Added guff (16/04/12)

It sounds like your problem is that no guarantee that generally you can link C++ code that''s compiled with two different compilers. Sometimes you can get away with it especially if you only pass pure (no implementation code or abstract) interface classes - i.e. all the code for the class is either in the DLL or it''s in the calling EXE.

If it''s not then the binary layout of the object can be different on either side of the EXE/DLL boundary as different compilers compile the same code in different ways AND the vendor might have changed the implementation of the class. MS have had to maul their standard library a fair bit to make it comply to the C++11 standard between VC++2008 and 2010. Where the code compiled with 2010 expects to find the vector''s data pointer the code compiled with 2008 compiler might have stuck the size at the same address, the implementations swapping the data members around for some reason them around for some reason.

So, what can you do about this then?

The first thing you can try is pass an abstract class that contains a vector<whatever> and exports a couple of pointers to the start and end of the vector.

An interface like:

class vector_int_marshaller
{
    public:
        virtual int *start_of_vector() const = 0;
        virtual int *end_of_vector() const = 0;
};



并在DLL(VC ++ 2005)端实现它:



And implement it on the DLL (VC++2005) side:

class vector_int_marshaller_imp : public vector_int_marshaller
{
    public:
        vector_int_marshaller_imp( const std::vector<int> &to_marshal ) : to_marshal_( to_marshal )
        {
        }

        virtual int *start_of_vector() const
        {
            return &to_marshal_[0];
        }

        virtual int *end_of_vector() const
        {
            return start_of_vector + to_marshal_.size();
        }

    private:
        const std::vector<int> &to_marshal_;
};



最后将其转换为EXE中的VC ++ 2010向量:



And finally convert it into a VC++2010 vector in the EXE:

std::vector<int> data( vec->start_of_vector(), vec->end_of_vector() );



只需使用内置类型和v_table,您就可以摆脱这种情况,因为MS仍必须支持COM(ho,ho,ho :-)),因此必须保持某种格式.

说了这么多,最好的办法就是抓住您使用的C ++代码的每一部分,并使用相同的编译器进行编译.绝对没有纯C接口的所有内容.

希望这些附加功能有所帮助,我错过了您之前使用多个编译器进行构建的地步,



更正了一些错字和一些不清楚的文字

EditII:标点符号失败,重写其中一部分以使其更清晰. grr ...

编辑III:刚注意到编辑器留下一些杂乱无章的东西



You can get away with this as you''re only using built in types AND a v_table, which MS have to keep in a certain format as they still support COM (ho, ho, ho :-) ).

Having said all that the best thing to do is grab every bit of C++ code you use and compile it with the same compiler. Absolutely everything that doesn''t have a pure C interface.

Hope the additions help, I missed the point that you were building with multiple compilers before,

Ash

Corrected a few typos and unclear bits of text

EditII: Punctuation fail, rewrote a chunk of it to try and make it clearer. grr...

Edit III: Just noticed some stray guff the editor left lying around


对于N原因,跨模块传递标准模板库对象并不安全.它们只是不会在模块边界之间保持和中断.

另一种方法是将它们分解成类并传递
For N-reasons, it is not safe to pass Standard template library objects across modules. They just wont hold and break between module boundaries.

The alternative is the break them up into classes and pass around


我的答案仅在我的问题上,问题在于编译器兼容性.
诺基亚已经在4月11日发布了带有msvc2010编译器的Qt 4.8.1,我不知道它们的最新版本,所以现在已经在Qt的msvc2010编译器中编译了我的DLL,并且在VC2010 MFC中可以正常使用.
求助.
My answer was in my question only, problem is with Compiler Compatibility.
Nokia has release Qt 4.8.1 on April 11 which comes with msvc2010 Compiler, i was not knowing about their recent release, so now have compiled my DLL in msvc2010 Compiler in Qt and it works fine in VC2010 MFC.
Thax for help.


这篇关于从vc ++ 2010 App无法访问vector相同的代码在Qt App中可以正常工作.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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