Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7083751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:13:00+00:00 2026-05-28T07:13:00+00:00

I am porting visual c++ 6 application to python using ctypes. It uses same

  • 0

I am porting visual c++ 6 application to python using ctypes.
It uses same windows library dll. and I am only porting windows forms to python application.

and I am stucked when I am sending cpp structure to dll in python.

I have tried to find solution on the struct differeces. from cpp code, there is a struct tagging(tagTest) but there is no equivalent in python code. is there any solution for that?

this is cpp that I want to port from.

========================cpp part================================

BOOL CSMessage(HWND hWnd, const char* string1, const char* string2, const char* ltagTest, int structlen);

typedef struct tagTest
{
    char item1                     [  8];   char _item1;
    char item2                     [  1];   char _item2;
} TcTest, *LPTest;  

void Initiator()
{
    TcTest  ctest   ={0};
    memset(&ctest,0x20,sizeof TcTest);
    move(ctest.item1,"TESTSTRI");
    move(ctest.item2,"1");

    mhandler.CSMessage(GetSafeHwnd(),"String","Call",(char*)&ctest,sizeof TcTest);
}

=========================pythonpart============================

csMessageProto=ctypes.WINFUNCTYPE(BOOL, HWND, c_char_p, c_char_p, c_char_p, c_int)
csMessageParams=((1,"hWnd",0),(1,"string1",0),(1,"string2",0),(1,"ltagTest",0),(1,"structlen",0))
CSMessage=csMessageProto(("CSMessage",testDll), csMessageParams)

class tagTest(ctypes.Structure):
    _fields_ = [
        ('item1', c_char*8),('_item1', c_char),
        ('item2', c_char*1),('_item2', c_char)
    ]
PtagTest=ctypes.POINTER(tagTest)

utestblock=tagTest()
utestblock.item1=str("TESTSTRI")
utestblock.item2=str(1)

CSMessage(hwnd, "String", "Call", ctypes.byref(utestblock), ctypes.sizeof(utestblock));

revision history

  1. added declaration of dll function both cpp and python
  2. revised with @avl_sweden suggestions

=====================result==============================

after applying @avl_sweden’s suggestion,
After test, the error msg is this.

ctypes.ArgumentError: argument 4: <type 'exceptions.TypeError'>: wrong type

and for the type

print "Is Type : "+str(ctypes.byref(utestblock))

Is Type : <cparam 'P' (023e5968)>

=========================================================

so it’s looks like types are different in def and instance.

in first line in python code

1: csMessageProto=ctypes.WINFUNCTYPE(BOOL, HWND, c_char_p, c_char_p, c_char_p, c_int)

the fourth c_char_p is not matching

16: CSMessage(hwnd, "String", "Call", ctypes.byref(utestblock), ctypes.sizeof(utestblock));

to match the differences.

I changed code to

1: csMessageProto=ctypes.WINFUNCTYPE(BOOL, HWND, c_char_p, PtagTest, c_char_p, c_int)

and code returns no error. but I cannot find if it is sent through. becuase there was no return from dll.

do you think this is the right solution?

=====================================================================================

i have been tested and it seems it is right solution.

Thank you again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:13:01+00:00Added an answer on May 28, 2026 at 7:13 am

    I see four things wrong here:

    First, your python Structure definition doesn’t match the one in the C-code snippet. The python one should be:

    class tagTest(ctypes.Structure):
        _fields_ = [
            ('item1', c_char*8), ('_item1',c_char),
            ('item2', c_char*1), ('_item2',c_char)
        ]
    

    The second thing I see, is that you’re sending a pointer to a pointer to the CSMessage function.

    Could you try:

    utestblock=tagTest()
    utestblock.item1=str("TESTSTRI")
    utestblock.item2=str(1)
    
    loadeddll.CSMessage(hwnd, "String", "Call", ctypes.byref(utestblock),
                        ctypes.sizeof(utestblock))
    

    The third issue is less severe, and this is that the string "TESTSTRING" does not fit into 8 bytes.

    The fourth issue I see, is that the types of the default parameters is wrong.

    Try this instead:

    csMessageParams=((1, "hWnd", 0), (1, "string1", ""), (1, "string2", ""),
                     (1, "ltagTest", ""), (1, "structlen", 0))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm porting a Java library to C#. I'm using Visual Studio 2008, so I
I'm porting a C# application which is completely written in windows under Visual studio
Visual Studio C++ 2005 Windows XP I am creating this DLL library. The DLL
I have windows application build using Visual C++. Its being build and run on
im porting c code to windows 32 bit using visual studio express now i
I'm working on porting a Visual C++ application to GCC (should build on MingW
While porting a desktop application to windows mobile I've reached the following error: Error
I'm porting an application I developed in Visual Studio 2008 over to Cocoa. I'm
I have a simple standalone application written in Visual Basic that I'm porting to
In general, I have been having difficulties porting my .NET Windows Forms applications created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.