0

I have a C++ DLL. I want to make a Flask server to call the functions in this DLL. So I use ctypes inside Flask. Here is my code in views.py:

from flask import render_template
from MusicServer_Flask import app
from ctypes import *
import os

#load Dll:
cppdll = CDLL("C:\\VS_projects\\MusicServer_Flask\\NetServerInterface.dll")

#wrapping DLL function:
initnetwork = getattr(cppdll, "?InitNetwork@@YAHQAD0H@Z") #The function can be accessed successfully
initnetwork.argtypes = [c_char_p,c_char_p,c_int]
initnetwork.restype = wintypes.BOOL

#define route:
@app.route('/InitNetwork/<LocalIP>/<ServerIP>/<LocalDeviceID>')
def InitNetwork(LocalIP, ServerIP, LocalDeviceID):
    return initnetwork(LocalIP, ServerIP, LocalDeviceID)

With the help of this question, I can call this function in Python interactice window successfully using this:InitNetwork(b"192.168.1.101",b"192.168.1.101",555): enter image description here However, when I run the Flask project and enter this route:http://192.168.1.102:8081/InitNetwork/b"192.168.1.102"/b"22192.168.1.102"/555 It gives me an error like this: enter image description here It seems that the b"192.168.1.102" becomes b%22192.168.1.102%22 in the requested URL. Why this happens? How can I use the right URL to call this function?

Thank you for your attention.

Edit:

Thanks to @Paula Thomas 's answer, I think I moved one step towards the answer.I changed the code as below to convert the first and second input parameters into bytes:

@app.route('/InitNetwork/<LocalIP>/<ServerIP>/<LocalDeviceID>')
def InitNetwork(LocalIP, ServerIP, LocalDeviceID):
    b_LocalIP = bytes(LocalIP,'utf-8')
    b_ServerIP = bytes(ServerIP,'utf-8')
    return initnetwork(b_LocalIP, b_ServerIP, LocalDeviceID)

However, neither http://172.16.4.218:8081/InitNetwork/172.16.4.218/172.16.4.218/555 nor http://172.16.4.218:8081/InitNetwork/"172.16.4.218"/"172.16.4.218"/555 works. Still gives me wrong type error. Would somebody please help?

YQ.Wang
  • 1,090
  • 1
  • 17
  • 43

1 Answers1

0

I think you'll find that Flask is passing you strings (type str) whereas your C or C++ function is expecting bytes (hence the 'b' before two of the parameters) for the first two parameters and an int for the third. It is your responsibility to convert them.

Paula Thomas
  • 1,152
  • 1
  • 8
  • 13